Design File System

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

You are asked to design a file system that allows you to create new paths and associate them with different values.

The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, /leetcode and /leetcode/problems are valid paths while an empty string "" and / are not.

Implement the FileSystem class:

  • createPath(string path, int value) — Creates a new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn't exist.
  • get(string path) — Returns the value associated with the path, or returns -1 if the path doesn't exist.

Examples

Example 1:

Input: operations = ["FileSystem","createPath","createPath","get","createPath","get"], operands = [[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",0],["/c"]]

Output: [null,true,true,2,false,-1]

Explanation: createPath("/leet", 1) creates path /leet with value 1. createPath("/leet/code", 2) creates path /leet/code with value 2. get("/leet/code") returns 2. createPath("/c/d", 0) fails because parent /c doesn't exist. get("/c") returns -1 because /c doesn't exist.

Example 2:

Input: operations = ["FileSystem","createPath","createPath","get","createPath","get"], operands = [[],["/a",1],["/a/b",2],["/a/b"],["/a/b",3],["/a/b"]]

Output: [null,true,true,2,false,2]

Explanation: createPath("/a/b", 3) fails because /a/b already exists. get("/a/b") still returns 2.

Constraints

  • The number of calls to the two functions is less than or equal to 10^4 in total
  • 2 <= path.length <= 100
  • 1 <= value <= 10^9
Source: Trie pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle