Skip to content

Latest commit

 

History

History
69 lines (47 loc) · 1.62 KB

_1166. Design File System.md

File metadata and controls

69 lines (47 loc) · 1.62 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 28, 2024

Last updated : June 28, 2024


Related Topics : Hash Table, String, Design, Trie

Acceptance Rate : 63.37 %


Solutions

Python

class FileSystem:

    def __init__(self):
        self.trie = {}

    def createPath(self, path: str, value: int) -> bool:
        pathChunks = path.split('/')[1:]

        curr = self.trie
        for i in range(len(pathChunks) - 1) :
            if pathChunks[i] not in curr :
                return False
            curr = curr[pathChunks[i]]

        if pathChunks[-1] in curr and 'value' in curr[pathChunks[-1]] :
            return False

        curr[pathChunks[-1]] = {}
        curr[pathChunks[-1]]['value'] = value
        return True

    def get(self, path: str) -> int:
        pathChunks = path.split('/')[1:]

        curr = self.trie
        for chunk in pathChunks :
            if chunk not in curr :
                return -1
            curr = curr[chunk]
        
        if 'value' in curr :
            return curr['value']
        return -1
        



# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.createPath(path,value)
# param_2 = obj.get(path)