1832. Check if the Sentence Is Pangram
· 약 2분
[Check if the Sentence Is Pangram - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)
소문자로 이루어진 영문 문자열이 주어진다.
주어진 문자열이 알파벳 26개가 모두 1번 이상 출현한다면 True를, 그렇지 않다면 False를 반환하는 문제다.
1. Set, discard()
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
alphabet_set = set([chr(i) for i in range(97, 123)])
for char in sentence:
alphabet_set.discard(char)
return True if len(alphabet_set) == 0 else False


간단하지만 다양한 방법으로 해결할 수 있다.
소문자 알파벳만으로 이루어진 집합을 만들고, 문자열의 문자를 순회하면서 해당 문자가 존재하면 집합에서 제거한다.
이 작업 후 집합의 크기가 0이면 True를, 그렇지 않다면 False를 반환한다.
보통 Python에서 집합 사용할 때 remove()를 많이 사용하는데 이는 해당 키가 없으면 KeyError를 발생시킨다.
키가 존재하는 경우엔 제거하고, 그렇지 않다면 아무 일도 일어나지 않게 하고 싶을 땐 discard()를 사용하면 불필요한 조건문을 사용하지 않을 수 있다.