Leetcode 1524 Solution

This article provides solution to leetcode question 1524 (string-matching-in-an-array)

https://leetcode.com/problems/string-matching-in-an-array

Solution

class Solution:
    def stringMatching(self, words: List[str]) -> List[str]:
        words.sort(key=lambda word: len(word))

        ans = []
        for i, word in enumerate(words):
            for j in range(i + 1, len(words)):
                if word in words[j]:
                    ans.append(word)
                    break
        return ans