๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/17677
ํ์ด
Counter ๋ฅผ ์ฌ์ฉํ ํ์ด
# [Programmers] [KAKAO BLIND RECRUITMENT] [Python] Level2_[1์ฐจ] ๋ด์ค ํด๋ฌ์คํฐ๋ง
from collections import Counter
def solution(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
new_str1 = []
new_str2 = []
for i in range(1, len(str1)):
if str1[i].isalpha() and str1[i-1].isalpha():
new_str1.append(str1[i-1] + str1[i])
for j in range(1, len(str2)):
if str2[j].isalpha() and str2[j-1].isalpha():
new_str2.append(str2[j-1] + str2[j])
Counter1 = Counter(new_str1)
Counter2 = Counter(new_str2)
intersection = list((Counter1 & Counter2).elements())
union = list((Counter1 | Counter2).elements())
if len(union) == 0 and len(intersection) == 0:
return 65536
return int(len(intersection) / len(union) * 65536)
print(solution('aa1+aa2', 'AAAA12'))
Counter๋ฅผ ์ฌ์ฉํ์ง ์์ ํ์ด
# [Programmers] [KAKAO BLIND RECRUITMENT] [Python] Level2_[1์ฐจ] ๋ด์ค ํด๋ฌ์คํฐ๋ง
def solution2(str1, str2):
answer = 0
str1 = str1.lower()
str2 = str2.lower()
new_str1 = []
new_str2 = []
intersection = [] # ๊ต์งํฉ
union = [] # ํฉ์งํฉ
for i in range(1, len(str1)):
if str1[i].isalpha() and str1[i-1].isalpha():
new_str1.append(str1[i-1] + str1[i])
for j in range(1, len(str2)):
if str2[j].isalpha() and str2[j-1].isalpha():
new_str2.append(str2[j-1] + str2[j])
for string in new_str1:
if string in new_str2:
intersection.append(string)
union.append(string)
new_str2.remove(string)
else:
union.append(string)
for k in range(len(new_str2)):
union.append(new_str2[k])
if len(union) == 0 and len(intersection) == 0:
return 65536
return int((len(intersection) / len(union)) * 65536)
print(solution2('aa1+aa2', 'AAAA12'))
ํฉ์งํฉ๊ณผ ๊ต์งํฉ์ด 0 ์ผ๋๋ฅผ ์๊ฐํ์ง ์์ ์๊ฐ์ ๋ง์ด ๋ญ๋นํ์๋ค. ์ฒ์์๋ ํฉ์งํฉ์ด 0์ผ๊ฒฝ์ฐ๋ง ์๊ฐํ์ฌ ์ค๋ฅ๊ฐ ๋ฌ์๋ค.