• Feed
  • Explore
  • Ranking
/
/
    ๐Ÿงฉ์•Œ๊ณ ๋ฆฌ์ฆ˜

    [LeetCode] 819. Most Common Word (Python, Counter)

    ๋ฌธ์ž์—ด
    k
    kawaihachiwarae
    2025.12.22
    ยท
    4 min read

    819. Most Common Word

    Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

    The words in paragraph are case-insensitive and the answer should be returned in lowercase.

    Note that words can not contain punctuation symbols.

     

    Example 1:

    Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
    Output: "ball"
    Explanation: 
    "hit" occurs 3 times, but it is a banned word.
    "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
    Note that words in the paragraph are not case sensitive,
    that punctuation is ignored (even if adjacent to words, such as "ball,"), 
    and that "hit" isn't the answer even though it occurs more because it is banned.
    

    Example 2:

    Input: paragraph = "a.", banned = []
    Output: "a"
    

     

    Constraints:

    • 1 <= paragraph.length <= 1000

    • paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".

    • 0 <= banned.length <= 100

    • 1 <= banned[i].length <= 10

    • banned[i] consists of only lowercase English letters.


    819. Most Common Word

    ์ฃผ์–ด์ง„ ๋ฌธ์žฅ(paragraph)๊ณผ ๊ธˆ์ง€ ๋‹จ์–ด ๋ชฉ๋ก(banned)์ด ์ฃผ์–ด์งˆ ๋•Œ,
    ๊ธˆ์ง€๋˜์ง€ ์•Š์€ ๋‹จ์–ด ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

    ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ์ฃผ์š” ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    • ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ

    • ๋ฌธ์žฅ์—๋Š” ๊ณต๋ฐฑ, ๋งˆ์นจํ‘œ, ์‰ผํ‘œ, ๋А๋‚Œํ‘œ ๋“ฑ์˜ ๋ฌธ์žฅ๋ถ€ํ˜ธ๊ฐ€ ํฌํ•จ๋จ

    • ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋จ

    • ๋ฌธ์žฅ๋ถ€ํ˜ธ๋Š” ๋‹จ์–ด์— ์ธ์ ‘ํ•ด ์žˆ์–ด๋„ ๋ฌด์‹œํ•ด์•ผ ํ•จ

    • ์ •๋‹ต์€ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜๋กœ ๋ณด์žฅ๋จ

    • ๊ฒฐ๊ณผ๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ

    โœ”ํ’€์ด

    import re
    from collections import defaultdict
    
    class Solution:
        def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
    
            word_count = defaultdict(int)
            words = re.findall(r'[a-zA-Z]+', paragraph)
    
            for word in words:
                if word.lower() not in banned:
                    word_count[word.lower()] += 1
    
            return max(word_count, key=word_count.get)
    
    • [a-zA-Z]+
      โ†’ ์˜์–ด ์•ŒํŒŒ๋ฒณ์ด 1๊ธ€์ž ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„

      • ๋ฌธ์žฅ๋ถ€ํ˜ธ(, . ! ? ' ;)์™€ ๊ณต๋ฐฑ ์ž๋™ ์ œ๊ฑฐ

      • ๊ฒฐ๊ณผ๋Š” ์˜์–ด ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ

    • dictionary์˜ key ์ค‘ value(๋“ฑ์žฅ ํšŸ์ˆ˜)๊ฐ€ ๊ฐ€์žฅ ํฐ key ๋ฐ˜ํ™˜

    • ๋ฌธ์ œ์—์„œ ์ •๋‹ต์ด ์œ ์ผํ•จ์ด ๋ณด์žฅ๋˜๋ฏ€๋กœ ์ถ”๊ฐ€ ์ฒ˜๋ฆฌ ๋ถˆํ•„์š”

    ๐Ÿ’กCounter ์‚ฌ์šฉ ํ’€์ด

    import re
    from collections import Counter
    
    class Solution:
        def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
    
            banned_set = set(banned)
            words = re.findall(r'[a-zA-Z]+', paragraph.lower())
    
            counter = Counter(word for word in words if word not in banned_set)
    
            return counter.most_common(1)[0][0]
    
    • Counter, set์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ํšจ์œจ์ ์ธ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•จ

    • banned List๋ฅผ set์œผ๋กœ ๋ฐ”๊พผ๋‹ค -> not in ์—ฐ์‚ฐ์ด O(1)์ด ๋จ

    • ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ๋‹จ์–ด๋“ค๋งŒ ์†Œ๋ฌธ์ž๋กœ ์ถ”์ถœํ•ด์„œ words์— ์ €์žฅ

    • banned_set์— ์žˆ์ง€ ์•Š์€ ๋‹จ์–ด๋“ค์„ Counter๋กœ ์„ธ๊ธฐ

    • most_common์˜ ์ •์ฒด

      ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ

      ํ˜•์‹

      counter.most_common(n)
      
      • n โ†’ ์ƒ์œ„ ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ฌ์ง€

      • ๋ฐ˜ํ™˜๊ฐ’ โ†’ (์›์†Œ, ํšŸ์ˆ˜) ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ

      counter.most_common(1)[0][0]
      

      ๋‹จ๊ณ„๋ณ„ ํ•ด์„

      1. most_common(1)
        โ†’ [('ball', 2)]

      2. [0]
        โ†’ ('ball', 2)

      3. [0]
        โ†’ 'ball'

    8601






    - ์ปฌ๋ ‰์…˜ ์•„ํ‹ฐํด