Created
December 15, 2019 06:15
-
-
Save inspirit941/a7bf088ddb30e7c98800fa6fcb2fc913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from collections import Counter | |
def solution(str1, str2): | |
# 소문자 2개씩만 추출하는 정규식 표현 | |
regex = re.compile('[a-z][a-z]') | |
# 대소문자 구분을 없애기 위해 전부 소문자로 변환 | |
str1, str2 = str1.lower(), str2.lower() | |
# 문자열 2개씩 묶은 데이터형으로 변환. | |
str1_ = regex.findall(" ".join(str1[i:i+2] for i in range(0, len(str1)))) | |
str2_ = regex.findall(" ".join(str2[i:i+2] for i in range(0, len(str2)))) | |
# 만약 둘 다 값이 없으면 문제에서 정의한 대로 1 * 65536 반환 | |
if len(str1_) == len(str2_) == 0: | |
return 1 * 65536 | |
# 각 문자열이 몇 번 등장했는지를 count. | |
str1_count, str2_count = Counter(str1_), Counter(str2_) | |
# 교집합과 합집합 만들기 | |
intersect, union = set(str1_) & set(str2_), set(str1_) | set(str2_) | |
# 다중집합의 교집합 원소의 개수. 더 적게 등장한 문자열 개수를 활용한다 | |
intersect = sum([min(str1_count[i], str2_count[i]) for i in intersect]) | |
# 다중집합의 합집합 원소의 개수. 더 많이 등장한 문자열 개수를 활용한다. | |
union = sum([max(str1_count[i], str2_count[i]) for i in union]) | |
return int((intersect / union) * 65536) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment