-
github.com/onlybooks/algorithm-interview
역시 코딩은 잘되면 너무 재밌어 🤣
819. Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example
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.
저번에도 봤듯이, 문자만 취하려면 정규식을 이용할 수 있다.
from collections import Counter res = Counter(input().upper()).most_common(2) if len(res)==1 or (len(res)==2 and res[0][1] != res[1][1]): print(res[0][0]) else: print("?")
'2020년 > 코테' 카테고리의 다른 글
[파이썬 알고리즘 인터뷰] 가장 킨 팰린드롬 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 그룹 애너그램 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 로그파일 재정렬 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 문자열 뒤집기 (0) 2020.12.15 [파이썬 알고리즘 인터뷰] 유효한 팰린드롬 (0) 2020.12.13