1832. Check if the Sentence Is Pangram

2021. 8. 4. 16:57·Algorithm
 

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

소문자로 이루어진 영문 문자열이 주어진다.

주어진 문자열이 알파벳 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()를 사용하면 불필요한 조건문을 사용하지 않을 수 있다.

 

 

'Algorithm' 카테고리의 다른 글

1828. Queries on Number of Points Inside a Circle  (0) 2021.08.06
1315. Sum of Nodes with Even-Valued Grandparent  (0) 2021.08.05
938. Range Sum of BST  (0) 2021.08.03
814. Binary Tree Pruning  (0) 2021.08.01
1038. Binary Search Tree to Greater Sum Tree  (0) 2021.07.31
'Algorithm' 카테고리의 다른 글
  • 1828. Queries on Number of Points Inside a Circle
  • 1315. Sum of Nodes with Even-Valued Grandparent
  • 938. Range Sum of BST
  • 814. Binary Tree Pruning
Doljae
Doljae
  • Doljae
    Zero to Hero
    Doljae
  • 전체
    오늘
    어제
    • 분류 전체보기 (350)
      • Programming (54)
      • Algorithm (161)
      • Review (103)
      • Career (8)
      • Diary (18)
      • Shorts (4)
      • Temp (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글 쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    sql
    BOJ
    2022
    2021
    개발자
    회고
    나는리뷰어다
    면접
    한빛미디어
    database
    백준
    line
    java
    mysql
    jpa
    AI
    leetcode
    라인
    코딩
    sql튜닝
    db
    나는 리뷰어다
    공채
    프로그래머스
    코딩테스트
    인프콘
    2023
    PYTHON
    컨퍼런스
    ChatGPT
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Doljae
1832. Check if the Sentence Is Pangram
상단으로

티스토리툴바