본문으로 바로가기
def solution(str1, str2):
    a, b = [], []
    
    for i in range(len(str1)-1):
        if ord('A') <= ord(str1[i].upper()) <= ord('Z') and ord('A') <= ord(str1[i+1].upper()) <= ord('Z'):
            a.append((str1[i]+str1[i+1]).upper())

    for i in range(len(str2)-1):
        if ord('A') <= ord(str2[i].upper()) <= ord('Z') and ord('A') <= ord(str2[i+1].upper()) <= ord('Z'):
            b.append((str2[i]+str2[i+1]).upper())
    
    u = set(a) | set(b)
    MAX = 0
    MIN = 0
    
    for i in u:
        MAX += max(a.count(i),b.count(i))
        MIN += min(a.count(i),b.count(i))
    
    if MAX != 0:
        return int((MIN / MAX) * 65536) 
    
    return 65536

파이썬의 set 자료구조를 사용하였다.

set을 모르면 굉장히 어려운 문제가 될 것 같다.

코딩테스트에서 set, dictionary 등을 자유자재로 활용할 수 있는 능력도 중요하다.

 

set끼리 연산할 때 

합집합 연산은 |

교집합 연산은 &

기호를 사용한다.

 

리스트 컴프리헨션을 사용하면 더 깔끔하게 풀 수 있다.