JAVA Solution, Trie Implementation


  • 0
    class FileNode{
        String fileName;
        boolean isFile;
        List<FileNode> children;
        StringBuilder sb;
        
        FileNode(String fileName, boolean isFile){
            this.fileName = fileName;
            this.isFile = isFile;
            
            children = new ArrayList<>();
            if(isFile){
                sb = new StringBuilder();    
            }
        }
        
        public void makeFile(){
            this.isFile = true;
            sb = new StringBuilder();
            return;
        }
        
        public boolean isFile(){
            return isFile;
        }
        
        public void append(String content){
            if(isFile){
                sb.append(content);
            }
            return;
        }
        
        public String getContent(){
            if(isFile){
                return sb.toString();
            }
            else{
                return "";
            }
        }
    }
    
    
    public class FileSystem {
        FileNode root;
        
        public FileSystem() {
            root = new FileNode("", false);
        }
        
        public List<String> ls(String path) {
            List<String> res = new ArrayList<>();
            FileNode node = search(path);
            
            if(node == null){
                return res;
            }
            
            if(node.isFile()){
                res.add(node.fileName);
                return res;
            }
            
            for(FileNode child : node.children){
                res.add(child.fileName);    
            }
            
            Collections.sort(res);
            return res;
        }
        
        public void mkdir(String path) {
            mkdirHelper(path);
        }
        
        public void addContentToFile(String filePath, String content) {
            FileNode node = mkdirHelper(filePath);
            if(!node.isFile()) node.makeFile();
            node.append(content);
            System.out.println(node.children.size());
        }
        
        public String readContentFromFile(String filePath) {
            FileNode node = search(filePath);
            if(node == null){
                return "";
            }
            
            return node.getContent();
        }
        
        public FileNode mkdirHelper(String path){
            FileNode node = root;
            int low = 1;
            for(int i = 1; i <= path.length(); i++){
                
                if(i == path.length() || path.charAt(i) == '/'){
                    String fileName = path.substring(low, i);
                    low = i + 1;
                    boolean isFound = false;
                    for(FileNode child : node.children){
                        if(child.fileName.equals(fileName)){
                            node = child;
                            isFound = true;
                            break;
                        }
                    }
                    
                    if(!isFound){
                        FileNode newNode = new FileNode(fileName, false);
                        node.children.add(newNode);
                        node = newNode;
                    }
                }
            }
            return node;
            
            
        }
        
        public FileNode search(String path){
            FileNode node = root;
            if(path.equals("/")){
                return node;
            }
            int low = 1; 
            for(int i = 1; i <= path.length(); i++){
                if(i == path.length() || path.charAt(i) == '/'){
                    String fileName = path.substring(low, i);
                    low = i + 1;
                    boolean isFound = false;
                    for(FileNode child : node.children){
                        if(child.fileName.equals(fileName)){
                            node = child;
                            isFound = true;
                            break;
                        }
                    }
                    
                    if(!isFound){
                        return null;
                    }
                }    
            }
            
            return node;
        }
    }
    
    /**
     * Your FileSystem object will be instantiated and called as such:
     * FileSystem obj = new FileSystem();
     * List<String> param_1 = obj.ls(path);
     * obj.mkdir(path);
     * obj.addContentToFile(filePath,content);
     * String param_4 = obj.readContentFromFile(filePath);
     */
    

Log in to reply
 

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