파비의 매일매일 공부기록

2023.01.27 Today's Challenge 본문

Problem Solving/LeetCode

2023.01.27 Today's Challenge

fabichoi 2023. 1. 27. 23:45

https://leetcode.com/problems/concatenated-words/

 

Concatenated Words - LeetCode

Concatenated Words - Given an array of strings words (without duplicates), return all the concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

leetcode.com

class Solution:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
        def can(w, dit):
            for i in range(mini, len(w)):
                lf = w[:i]
                rt = w[i:]
                if lf in dit:
                    if rt in dit or can(rt, dit):
                        return True
            return False
        
        res = []
        dit = set(list(words))
        mini = 10000

        for w in words:
            mini = min(len(w), mini)
        
        for w in words:
            if can(w, dit):
                res.append(w)
        
        return res
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.01.29 Today's Challenge  (0) 2023.01.29
2023.01.28 Today's Challenge  (0) 2023.01.28
2023.01.26 Today's Challenge  (0) 2023.01.26
2023.01.25 Today's Challenge  (0) 2023.01.25
2023.01.24 Today's Challenge  (0) 2023.01.24
Comments