๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/LEETCODE

Group Anagrams | LeetCode 778 | Python3 ๐Ÿ

๋ฐ˜์‘ํ˜•

 

๐Ÿ“„ ๋ชฉ์ฐจ

     

    ๐Ÿค” ๋ฌธ์ œ : Group Anagrams | LeetCode 778

    ๋ฌธ์ œ: https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/778/
    ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ, ๊ฐ™์€ ๋ฌธ์ž ๊ตฌ์„ฑ์œผ๋กœ ์ˆœ์„œ๋งŒ ๋ฐ”๋€ ๋ฌธ์ž์—ด์„ ๋ฌถ์–ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

    ์˜ˆ๋ฅผ๋“ค์–ด abc, acb, bac, bca, cab, cba๊ฐ€ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ž…๋‹ˆ๋‹ค. 

     

    ๐Ÿ’ก ํ’€์ด

    1. ์ ‘๊ทผ - '๊ฐ™์€ ๋ฌธ์ž๋“ค๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด'์ž„์„ ์–ด๋–ป๊ฒŒ ํŒ๋‹จํ• ๊นŒ? 

    ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๊ฐ™์€ ๋ฌธ์ž๋“ค๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ž„์„ ์–ด๋–ป๊ฒŒ ํŒ๋‹จํ• ์ง€์ž…๋‹ˆ๋‹ค.

    ๊ฐ™์€ ๋ฌธ์ž๋“ค๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์€ ๊ฐ™์€ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์„œ ํŒ๋‹จํ•˜๋ฉด ๋˜๊ฒ ์ฃ . 

    ์ €๋Š” ๋ฌธ์ž์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    2.  ํ’€์ด

    from typing import List
    class Solution:
        def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
            sorted_string_set = {}
            grouped_str_list = []
            targetIndex = 0
            for s in strs:
                sorted_str = str(sorted(s))
                if sorted_str in sorted_string_set:
                    grouped_str_list[ sorted_string_set[ sorted_str ] ].append(s)
                else:
                    sorted_string_set[ sorted_str ] = targetIndex
                    targetIndex += 1
                    grouped_str_list.append([s])
    
            return grouped_str_list
    
    print(Solution().groupAnagrams(["eat","tea","tan","ate","nat","bat"]))

     

    Time Complexity
    ๋ฌธ์ž์—ด ์ •๋ ฌ : O(klogk), k๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด
    set์—์„œ ๋ฌธ์ž์—ด ํƒ์ƒ‰ : O(1)
    Space Complexity
    ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด๋งŒ set์— ์ค‘๋ณต์—†์ด ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)์ด๊ฒ ๋„ค์š”. (๋ชจ๋“  ๋ฌธ์ž์—ด์ด anagram์ด ์•„๋‹Œ ๊ฒฝ์šฐ)

     

    3. ์ž๋ฃŒ๊ตฌ์กฐ ๋ฐ ํ•จ์ˆ˜ ์„ ํƒ์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ์˜ ์ฐจ์ด 

    ์ฒ˜์Œ์—๋„ ๋กœ์ง์€ ๊ฑฐ์˜ ๋™์ผํ–ˆ๋Š”๋ฐ, ์ˆ˜ํ–‰์‹œ๊ฐ„์ด ๊ฑฐ์˜ 9๋ฐฐ๋‚˜ ํฌ๊ฒŒ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. 

     

    ๋‹ค๋ฅธ์ ์€ ์•„๋ž˜ ๋‘๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 

    1. sorted_string_list -> set (906ms -> 173ms)

    2.  ''.join(sorted(str)) -> str(sorted(s)) (173ms -> 112ms)

     

    ์•„๋ž˜๋Š” ์ฒ˜์Œ ์ œ์ถœํ•œ ์ฝ”๋“œ์™€ ์„ฑ๋Šฅ์ž…๋‹ˆ๋‹ค.

    from typing import List
    
    class Solution:
        def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
            sorted_string_set = []
            grouped_str_list = []
            targetIndex = 0
            for str in strs:
                sorted_str = ''.join(sorted(str))
                group_id = sorted_string_set.index(sorted_str) if sorted_str in sorted_string_set else -1
                if group_id < 0 :
                    group_id = targetIndex
                    targetIndex += 1
                    sorted_string_set.append(sorted_str)
                    grouped_str_list.append([str])
                else:
                    grouped_str_list[group_id].append(str)
    
                # print(sorted_string_set)
            # print(grouped_str_list)
            return grouped_str_list

     

     

    python์—์„œ set๊ณผ dictionary๋Š” Hashmap ๊ธฐ๋ฐ˜์ด๊ธฐ์— insert์— O(1)๋ฐ–์— ๊ฑธ๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    ๋ฐ˜๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ์˜ time complexity๋Š”  O(n) ์ž…๋‹ˆ๋‹ค.

     

    ์ฐธ๊ณ ) ํŒŒ์ด์ฌ ๊ธฐ๋ณธ ์ž๋ฃŒ๊ตฌ์กฐ์˜ time complexity 

    https://wiki.python.org/moin/TimeComplexity

     

    TimeComplexity - Python Wiki

    This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. Howe

    wiki.python.org

     

    ๋ฐ˜์‘ํ˜•