각 문자열들에서 공통 문자열을 찾는다
사람들이 주문한 메뉴들에서 개수만큼의 조합
을 뽑아 주문 횟수를 카운팅한다.
메뉴 개수
를 key값
으로 가지는 딕셔너리를 이용해 조합별 횟수를 저장하면 최댓값 찾기가 편리하다.
from itertools import combinations
def solution(orders, course):
answer = []
menus = {} # key: 메뉴 개수, value: { key: 메뉴 조합, value: 메뉴 주문 횟수}
for order in orders:
for c in course:
for comb in combinations(sorted(order), c):
if c not in menus:
menus[c] = {}
menu = "".join(comb) # ["A", "C"] -> "AC"
if menu not in menus[c]:
menus[c][menu] = 0
menus[c][menu] += 1
for c in course:
if c in menus:
m = max(menus[c].values())
if m >= 2: # 2명 이상 주문한 조합일 때
answer.extend([k for k, v in menus[c].items() if menus[c][k] == m])
return sorted(answer)코드를 입력하세요
참고
파이썬 딕셔너리 최댓값 찾기 (1개와 1개이상) - https://bio-info.tistory.com/40
파이썬 조합 - https://wikidocs.net/106964