Design File System

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:

 

Example 1:

Input: 
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output: 
[null,true,1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1

Example 2:

Input: 
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output: 
[null,true,true,2,false,-1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist.

 

Constraints:


Solution:

class FileSystem {
    Map<String, Integer> map;
    
    public FileSystem() {
        this.map = new HashMap();        
    }
    
    public boolean createPath(String path, int value) {
        if (map.containsKey(path)) return false;
        String[] arr = path.split("/");
        // for (String s : arr) System.out.print(s + ", ");
        if (arr.length == 0) return false;
        if (arr.length == 2) {
            map.put(path, value);
            return true;
        }
        StringBuilder parent = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i ++) {
            if (!arr[i].equals("")) {
                parent.append("/");
                parent.append(arr[i]);
            }
        }
        if (!map.containsKey(parent.toString())) return false;
        map.put(path, value);
        return true;
    }
    
    public int get(String path) {
        if (!map.containsKey(path)) return -1;
        return map.get(path);
    }
}

/**
 * Your FileSystem object will be instantiated and called as such:
 * FileSystem obj = new FileSystem();
 * boolean param_1 = obj.createPath(path,value);
 * int param_2 = obj.get(path);
 */