class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
# magazine에 있는 문자들의 빈도를 저장할 딕셔너리
countM = {}
# magazine 문자열에서 각 문자의 빈도를 계산
for c in magazine:
countM[c] = 1 + countM.get(c, 0) # 딕셔너리에 c가 없으면 기본값은 0
# ransomNote 문자열의 각 문자를 확인
for w in ransomNote:
# 현재 문자가 magazine에서 충분히 남아있는지 확인
if w in countM and countM[w] > 0:
countM[w] -= 1 # 사용한 문자를 magazine에서 하나 소모
else:
# magazine에서 필요한 문자가 없거나 부족하면 False 반환
return False
# ransomNote의 모든 문자를 성공적으로 처리하면 True 반환
return True
'LeetCode > Top Interview 150' 카테고리의 다른 글
290. Word Pattern (0) | 2024.11.30 |
---|---|
205. Isomorphic Strings (1) | 2024.11.30 |
48. Rotate Image (0) | 2024.11.30 |
54. Spiral Matrix (0) | 2024.11.30 |
36. Valid Sudoku (0) | 2024.11.30 |