사용자 도구

사이트 도구


ps:problems:programmers:17677

[1차] 뉴스 클러스터링

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호17677
문제명[1차] 뉴스 클러스터링
레벨Level 2
분류

기초

시간복잡도O(n+m)
인풋사이즈n<=1000, m<=1000
사용한 언어Python
해결날짜2022/01/09

풀이

  • 그냥 시키는대로 구현하면 되는 문제
  • 파이썬에서는 collections.Counter을 다중집합처럼 사용할수 있다. 교집합과 합집합도 쉽게 구할 수 있다. python 3.10 이상에서는 원소의 총 갯수도 바로 구할수 있는 total() 메소드도 제공하지만, 프로그래머스에서는 아직 지원하지 않아서 그냥 sum으로 구했다.

코드

"""Solution code for "Programmers 17677. [1차] 뉴스 클러스터링".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/17677
- Solution link: http://www.teferi.net/ps/problems/programmers/17677
"""

import collections

MULTIPLIER = 65536


def solution(str1, str2):
    str1, str2 = str1.upper(), str2.upper()
    counter1 = collections.Counter(
        x + y for x, y in zip(str1, str1[1:]) if (x + y).isalpha())
    counter2 = collections.Counter(
        x + y for x, y in zip(str2, str2[1:]) if (x + y).isalpha())
    intersection = sum((counter1 & counter2).values())
    union = sum((counter1 | counter2).values())
    return MULTIPLIER if union == 0 else MULTIPLIER * intersection // union

토론

댓글을 입력하세요:
G R W R H
 
ps/problems/programmers/17677.txt · 마지막으로 수정됨: 2022/01/10 02:21 저자 teferi