Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[프로그래머스] Lv.2 [1차] 뉴스 클러스터링 [Python, 파이썬] KAKAO 본문
- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/17677#qna
- 교집합, 합집합, 공집합, 다중집합
- 다중집합이란 말 때문에 집합연산자 |, &을 사용 안 했지만 새로운 교집합, 합집합을 만들어서 이 원소들로 개수만 세면 되는 거였다 😂
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)
'프로그래머스' 카테고리의 다른 글
[프로그래머스] Lv.2 k진수에서 소수 개수 구하기 [Python, 파이썬] KAKAO (0) | 2023.09.26 |
---|---|
[프로그래머스] Lv.1 실패율 [Python, 파이썬] KAKAO (0) | 2023.09.24 |
[프로그래머스] Lv.1 [1차] 비밀지도 [Python, 파이썬] KAKAO (0) | 2023.09.22 |
[프로그래머스] Lv.1 숫자 문자열과 영단어 [Python, 파이썬] KAKAO (0) | 2023.09.22 |
[프로그래머스] Lv.3 헤비 유저가 소유한 장소 [Oracle, 오라클] (0) | 2023.09.22 |
Comments