[LeetCode] 1684. Count the Number of Consistent Strings (Python)
LeetCode - Problems - Algorithms - 1684. Count the Number of Consistent Strings
Problem Description
You are given a string allowed consisting of distinct characters and an array of strings words.
A string is consistent if all characters in the string appear in the string allowed.
Return the number of consistent strings in the array words.
Example:
Constraints:
My Solution (Python)
class Solution(object):
def countConsistentStrings(self, allowed, words):
"""
:type allowed: str
:type words: List[str]
:rtype: int
"""
answer = len(words)
for word in words:
for l in word:
if l not in allowed:
answer -= 1
break
return answer
์ฃผ์ด์ง ๋จ์ด๋ค์ด ํ์ฉ๋ ์ํ๋ฒณ๋ง์ผ๋ก ์ด๋ฃจ์ด์ ธ์๋ ๋จ์ด๋ฅผ ์ฐพ๊ณ , ์กฐ๊ฑด์ ์ถฉ์กฑํ๋ ๋จ์ด์ ๊ฐฏ์๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ์์ฑํด์ผํ๋ค.
์ฐ์ ์ ์ ๋ต์ผ๋ก ์ ์ถํ answer๋ฅผ words์ ๊ธธ์ด๋ก ์ ์ธํด์ค๋ค.
words ๋ฆฌ์คํธ์ ๋จ์ด๋ฅผ ์ฐจ๋ก๋๋ก ํ์ธํ๊ณ , ๊ทธ ์์ letter๋ค๊น์ง ํ์ธํ๋ ๋ ๊ฐ์ for๋ฌธ์ ์ด์ฉํ๋ค.
๋ง์ฝ์ ํ์ธํ๋ letter๊ฐ allowed์ ์๋ค๋ฉด, answer๋ฅผ 1 ๊ฐ์ ์ํค๊ณ ๋ด๋ถ for๋ฌธ์ ์ข ๋ฃํ๊ณ ์ธ๋ถ for๋ฌธ์ ๋ค์ ๋ฐ๋ณต์ ์ํํ๋ค.
์ ์ฒด ๋จ์ด์ ๊ฐฏ์์์ ์กฐ๊ฑด์ ์ถฉ์กฑํ์ง ์๋ ๋จ์ด๊ฐ ๋ฐ๊ฒฌ๋ ๋๋ง๋ค 1์ฉ ๊ฐ์์ํค๋ ๋ฐฉ๋ฒ์ ์ ํํ ์ด์ ๋, ํ ๋ฒ ํ์ฉ๋์ง ์์ letter๊ฐ ๋ฐ๊ฒฌ๋๋ฉด ๋๋จธ์ง letter๋ค์ ํ์ธํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
0์์ ๋จ์ด์ ๋ชจ๋ letter๊ฐ allowed์ ํฌํจ๋๋์ง ํ์ธํ ๋ค์ 1 ์ฉ ์ฆ๊ฐ์ํค๋ ๋ฐฉ๋ฒ๋ณด๋ค ๋ ์ ์ ํ์์ ๋ฐ๋ณต์ ์คํํ๋ฏ๋ก ํจ๊ณผ์ ์ด๋ค.