728x90

나의 풀이
import re
from collections import defaultdict
def solution(str1, str2):
pool1, pool2 = defaultdict(int), defaultdict(int)
union, intersection = defaultdict(int), defaultdict(int)
str1 = re.sub(r'[^a-z]', ' ', str1.lower())
str2 = re.sub(r'[^a-z]', ' ', str2.lower())
for i in range(1, len(str1)):
if str1[i - 1] != ' ' and str1[i] != ' ':
pool1[str1[i - 1: i + 1]] += 1
for i in range(1, len(str2)):
if str2[i - 1] != ' ' and str2[i] != ' ':
pool2[(str2[i - 1: i + 1])] += 1
for p in pool1:
if p in pool2:
union[p] = max(pool1[p], pool2[p])
else:
union[p] = pool1[p]
for p in pool2:
if p in pool1:
union[p] = max(pool1[p], pool2[p])
else:
union[p] = pool2[p]
for p in pool1:
if p in pool2:
intersection[p] = min(pool1[p], pool2[p])
if union:
return int(sum(intersection.values()) / sum(union.values()) * 65536)
else:
return 65536
집합을 이용한 풀이
import re
import math
def solution(str1, str2):
str1 = [str1[i:i+2].lower() for i in range(0, len(str1)-1) if not re.findall('[^a-zA-Z]+', str1[i:i+2])]
str2 = [str2[i:i+2].lower() for i in range(0, len(str2)-1) if not re.findall('[^a-zA-Z]+', str2[i:i+2])]
gyo = set(str1) & set(str2)
hap = set(str1) | set(str2)
if len(hap) == 0 :
return 65536
gyo_sum = sum([min(str1.count(gg), str2.count(gg)) for gg in gyo])
hap_sum = sum([max(str1.count(hh), str2.count(hh)) for hh in hap])
return math.floor((gyo_sum/hap_sum)*65536)
isaplha를 이용한 풀이
def solution(str1, str2):
list1 = [str1[n:n+2].lower() for n in range(len(str1)-1) if str1[n:n+2].isalpha()]
list2 = [str2[n:n+2].lower() for n in range(len(str2)-1) if str2[n:n+2].isalpha()]
tlist = set(list1) | set(list2)
res1 = [] #합집합
res2 = [] #교집합
if tlist:
for i in tlist:
res1.extend([i]*max(list1.count(i), list2.count(i)))
res2.extend([i]*min(list1.count(i), list2.count(i)))
answer = int(len(res2)/len(res1)*65536)
return answer
else:
return 65536반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] 수식 최대화 (0) | 2022.06.13 |
|---|---|
| [프로그래머스/파이썬] 거리두기 확인하기 (0) | 2022.06.12 |
| [프로그래머스/파이썬] 괄호 변환 (0) | 2022.06.11 |
| [프로그래머스/파이썬] 메뉴 리뉴얼 (0) | 2022.06.11 |
| [프로그래머스/파이썬] 행렬 테두리 회전하기 (0) | 2022.06.10 |