My java solution


  • 0
    S
     public String simplifyPath(String path) {
             Stack st = new Stack();
             String []pt = path.split("/");
             for(String s:pt)
             {
                 if(s.equals("")) continue;
                 if(s.matches("[.]{2}")) 
                 {
                     if(!st.empty())st.pop();
                     continue;
                 }
                 else if(!s.matches("\\.")) st.push(s);
             }
             if(st.empty()) return "/";
             StringBuilder spath=new StringBuilder();
             while(!st.empty())
             {
                spath.insert(0,st.pop());
                spath.insert(0,'/');
             }
             return spath.toString();
        }

Log in to reply
 

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