Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public String simplifyPath(String path) {
- LinkedList<String> stack = new LinkedList<String>();
- StringBuffer sb1 = new StringBuffer("");
- for(int i = 0 ; i < path.length(); i++)
- {
- if(path.charAt(i)=='/')
- {
- if(sb1.toString().equals(".."))
- {
- if(!stack.isEmpty())
- stack.removeLast();
- }else if(!sb1.toString().equals("")&&!sb1.toString().equals("."))
- stack.addLast(sb1.toString());
- sb1=new StringBuffer("");
- }else
- {
- sb1.append(path.charAt(i));
- }
- }
- if(sb1.toString().equals(".."))
- {
- if(!stack.isEmpty())
- stack.removeLast();
- }else if(!sb1.toString().equals("")&&!sb1.toString().equals("."))
- stack.addLast(sb1.toString());
- StringBuffer result = new StringBuffer("");
- if(stack.isEmpty()) return "/";
- for(String s: stack)
- {
- result = result.append("/");
- result = result.append(s);
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement