나의 개발일지

[프로그래머스] Lv.2 [1차] 뉴스 클러스터링 [Python, 파이썬] KAKAO 본문

프로그래머스

[프로그래머스] Lv.2 [1차] 뉴스 클러스터링 [Python, 파이썬] KAKAO

YoonJuHan 2023. 9. 24. 16:32

 

def makeSet(st, se):    # 다중 집합으로 변환
    for i in range(len(st)-1):
        x = st[i]
        y = st[i+1]
        if x.isalpha() and y.isalpha():
            se.append(x+y)
            
def solution(str1, str2):
    
    a, b = 0, 0 # 교집합 길이, 합집합 길이
    str1, str2 = str1.upper(), str2.upper()
    set1, set2 = [], []
    check = []
    makeSet(str1, set1)
    makeSet(str2, set2)
    
    if not set1 and set2:
        return 0
    elif not (set1 and set2): # 둘 다 공집합이면
        return 65536
        
    for i in range(len(set1)):  # 교집합 구하기
        if set1[i] in set2 and set1[i] not in check:
            a += min(set1.count(set1[i]), set2.count(set1[i]))
            check.append(set1[i])
            
    check = []
    for i in range(len(set1)):  # 합집합 구하기
        if set1[i] not in check:
            b += max(set1.count(set1[i]), set2.count(set1[i]))
            check.append(set1[i])
            
    for i in range(len(set2)):  # 합집합 구하기
        if set2[i] not in check:
            b += max(set1.count(set2[i]), set2.count(set2[i]))
            check.append(set2[i])

    return int(a / b * 65536)
Comments