Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Trie:
- class TrieNode:
- def __init__(self):
- self.child = {}
- self.val = -1
- def __init__(self):
- self.root = self.TrieNode()
- def insert(self, path: List[str], val):
- cur = self.root
- for file in path:
- if (file not in cur.child):
- cur.child[file] = self.TrieNode()
- cur = cur.child[file]
- cur.val = val
- return True
- def search(self, path: List[str]) -> int:
- cur = self.root
- for file in path:
- if file not in cur.child:
- return -1
- cur = cur.child[file]
- return cur.val
- class FileSystem:
- def __init__(self):
- self.trie = Trie()
- def createPath(self, path: str, value: int) -> bool:
- path = path.split("/")
- path = [x for x in path if x]
- tempPath = path.copy()
- tempPath.pop()
- if ((len(tempPath) > 0 and (self.trie.search(tempPath) == -1) or self.trie.search(path) != -1)):
- return False
- return self.trie.insert(path, value)
- def get(self, path: str) -> int:
- path = path.split("/")
- path = [x for x in path if x]
- val = self.trie.search(path)
- return val
- # Your FileSystem object will be instantiated and called as such:
- # obj = FileSystem()
- # param_1 = obj.createPath(path,value)
- # param_2 = obj.get(path)
- """
- a/b/c/d/e
- /
- a. b. c.
- a. c.
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement