소프트웨어에 대한 모든 것

LeetCode 풀기 - 1684. Count the Number of Consistent Strings 본문

알고리즘/LeetCode

LeetCode 풀기 - 1684. Count the Number of Consistent Strings

앤테바 2021. 10. 26. 07:56
반응형

1684. Count the Number of Consistent Strings

문제)

솔루션1)

  • allowed 단어에 words가 있는지 탐색
  • O(n2)
class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        # Brute-Force
        count = 0
        for word in words:                        
            count += 1            
            for i in range(len(word)):
                if word[i] not in allowed:
                    count -= 1
                    break
        return count

솔루션2)

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        allowed = set(allowed)
        return sum(all(letter in allowed for letter in word) for word in words)

 

반응형
Comments