Java TreeMap solution


  • 0
    // by fallcreek
    public class FileSystem {
    
        Obj root;
        Set<String> mkcache = new HashSet<>();
        public FileSystem() {
            root = new Obj("/",'d',"");
        }
        
        public List<String> ls(String path) {
            String[] pathList = path.split("/");
            List<String> sol = new ArrayList<>();
    
            Obj curr = root;
            for(String p : pathList){   // go to the last directory or file
                if(p.equals("")) continue; // split may cause the first path to be empty string
                curr = curr.children.get(p);
            }
            if(curr.type == 'f') sol.add(curr.name); // file, add file name
            else    for(Obj child : curr.children.values()) sol.add(child.name); // directory, add all directory/file name
            
            return sol;
       }
        
        public void mkdir(String path) {
            String[] pathList = path.split("/");
            Obj curr = root;
            for(String p : pathList){
                if(p.equals("")) continue;
                if(!curr.children.containsKey(p)){ // directory doesn't exist, create a new directory
                    curr.children.put(p, new Obj(p,'d',""));
                }
                curr = curr.children.get(p);
            }
            return;
        }
        
        public void addContentToFile(String filePath, String content) {
            String[] pathList = filePath.split("/");
            Obj curr = root;
            for(int i = 0; i < pathList.length - 1; i++){ // leave the last alone, since the last is a file may not exist
                String p = pathList[i];
                if(p.equals("")) continue;
                curr = curr.children.get(p);
            }
            
            String fileName = pathList[pathList.length-1];
            if(curr.children.containsKey(fileName)) curr.children.get(fileName).content.append(content); // exist, append
            else    curr.children.put(fileName, new Obj(fileName, 'f', content)); // doesn't exist, create new file
            return;   
        }
        
        public String readContentFromFile(String filePath) {
            String[] pathList = filePath.split("/"); 
            Obj curr = root;
            for(String p : pathList){
                if(p.equals("")) continue;
                curr = curr.children.get(p);
            }
            return curr.content.toString();
        }
    }
    
    class Obj{
       String name;
       char type; // ‘d’:directory, ‘f’:file
       StringBuilder content; // content for file
       TreeMap<String, Obj> children = new TreeMap<>();
       
       public Obj(String name, char type, String content){
           this.name = name;
           this.type = type;
           this.content = new StringBuilder(content);
       }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.