Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 9. 19. 23:45

https://leetcode.com/problems/find-duplicate-file-in-system/

 

Find Duplicate File in System - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

딱 봤을 땐, 어려워 보이는데
그냥 해쉬 맵 써서 풀면 끝.

class Solution:
    def findDuplicate(self, paths: List[str]) -> List[List[str]]:
        m = {}
        for path in paths:
            s_path = path.split()
            root = s_path[0] 
            for file in s_path[1:]:
                file_name, content = file.split('(')
                full_path = root + '/' + file_name
                if content in m:
                    m[content].append(full_path)
                else:
                    m[content] = [full_path]
        
        ans = []
        for content in m:
            if len(m[content]) > 1:
                ans.append(m[content])
        
        return ans
반응형