Python O(mn) without sorting


  • 0
    O
        def findLongestWord(self, s, d):
            """
            :type s: str
            :type d: List[str]
            :rtype: str
            """
            ans = ''
            for word in d:
                idx = 0
                for char in s:
                    if idx == len(word):
                        break
                    if char == word[idx]:
                        idx += 1
                    
                if idx != len(word):
                    continue
                
                if len(ans) < len(word) or (len(ans) == len(word) and word < ans):
                    ans = word
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.