Advertisement
nirajs

Untitled

Jan 11th, 2024
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. class Trie:
  2.     class TrieNode:
  3.         def __init__(self):
  4.             self.child = {}
  5.             self.val = -1
  6.    
  7.     def __init__(self):
  8.         self.root = self.TrieNode()
  9.    
  10.     def insert(self, path: List[str], val):
  11.         cur = self.root
  12.         for file in path:
  13.             if (file not in cur.child):
  14.                 cur.child[file] = self.TrieNode()
  15.             cur = cur.child[file]
  16.         cur.val = val
  17.         return True
  18.  
  19.  
  20.     def search(self, path: List[str]) -> int:
  21.         cur = self.root
  22.         for file in path:
  23.             if file not in cur.child:
  24.                 return -1
  25.             cur = cur.child[file]
  26.         return cur.val
  27.  
  28.  
  29. class FileSystem:
  30.  
  31.     def __init__(self):
  32.         self.trie = Trie()
  33.  
  34.     def createPath(self, path: str, value: int) -> bool:
  35.         path = path.split("/")
  36.         path = [x for x in path if x]
  37.         tempPath = path.copy()
  38.         tempPath.pop()
  39.  
  40.         if ((len(tempPath) > 0 and (self.trie.search(tempPath) == -1) or self.trie.search(path) != -1)):
  41.             return False
  42.         return self.trie.insert(path, value)
  43.  
  44.     def get(self, path: str) -> int:
  45.         path = path.split("/")
  46.         path = [x for x in path if x]
  47.         val = self.trie.search(path)
  48.         return val
  49.        
  50.  
  51.  
  52. # Your FileSystem object will be instantiated and called as such:
  53. # obj = FileSystem()
  54. # param_1 = obj.createPath(path,value)
  55. # param_2 = obj.get(path)
  56.  
  57.  
  58. """
  59.  
  60.  
  61. a/b/c/d/e
  62.  
  63.   /
  64.  
  65. a.     b.    c.
  66.  
  67. a.    c.  
  68.  
  69. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement