-
github.com/onlybooks/algorithm-interview
배고푸당 이거하고 밥먹어야지~
49. Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1
Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2
Input: strs = [""] Output: [[""]]
Example 3
Input: strs = ["a"] Output: [["a"]]
from collections import defaultdict class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: res = defaultdict(list) for s in strs: res[''.join(sorted(s))].append(s) return res.values()
str_1 = input() str_2 = input() from collections import Counter if sorted(str_1) != sorted(str_2): counter_str_1 = Counter(str_1) counter_str_2 = Counter(str_2) print(sum((counter_str_1-counter_str_2).values()) +sum((counter_str_2-counter_str_1).values())) else: print(0)
'2020년 > 코테' 카테고리의 다른 글
[파이썬 알고리즘 인터뷰] 배열 (0) 2020.12.20 [파이썬 알고리즘 인터뷰] 가장 킨 팰린드롬 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 가장 흔한 단어 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 로그파일 재정렬 (0) 2020.12.19 [파이썬 알고리즘 인터뷰] 문자열 뒤집기 (0) 2020.12.15