728x90
https://school.programmers.co.kr/learn/courses/30/lessons/17677
교집합일 때는 min을, 합집합일 때는 max로 하는 걸 어떻게 하지 고민하다가 너무 답답해서 구글링한 문제
그냥 Counter에서 &, |를 하면 해결된단다.....
그 후에 counter에서 elements를 통해서 문자들을 개수 만큼 만들고 그것의 개수를 센다.
코드 :
from collections import Counter
def solution(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
str1_lst = []
str2_lst = []
for i in range(1,len(str1)) :
tmp = str1[i-1:i+1]
if tmp.isalpha() : str1_lst.append(tmp)
for i in range(1,len(str2)) :
tmp = str2[i-1:i+1]
if tmp.isalpha() : str2_lst.append(tmp)
Counter1 = Counter(str1_lst)
Counter2 = Counter(str2_lst)
inter = list((Counter1 & Counter2).elements())
union = list((Counter1 | Counter2).elements())
if len(union) ==0 and len(inter) == 0 :
return 65536
else :
return int(len(inter) / len(union)*65536)
참고 블로그 :
https://dongdongfather.tistory.com/70
반응형
'알고리즘 > 프로그래머스문제풀이' 카테고리의 다른 글
[Python/프로그래머스]보석쇼핑_(KaKao)_[투포인터] (2) | 2022.10.01 |
---|---|
[Python/프로그래머스]순위검색_(KaKao)_[구현,bisect(이분탐색)] (0) | 2022.09.27 |
[Python/프로그래머스]튜플_(KaKao)_문제 이해가 잘 안되요 (0) | 2022.09.26 |
[Python/프로그래머스]아이템 줍기_(BFS) (0) | 2022.09.22 |
[Python/프로그래머스]게임 맵 최단거리_(BFS) (0) | 2022.09.22 |