public String simplifyPath(String path) {
if(path==null)
return "";
Stack<String> stack = new Stack<String>();
StringBuffer buf = new StringBuffer();
for(int i=0; i<path.length(); i++){
if(path.charAt(i)!='/')
buf.append(path.charAt(i));
else{
if(!buf.equals("")){
stack.push(buf.toString());
buf= new StringBuffer();
}
}
}
if(!buf.equals(""))
stack.push(buf.toString());
int cnt = 0;
String str = "";
while(!stack.isEmpty()){
String s = stack.pop();
if(s.equals( "."))
continue;
else if(s.equals(".."))
cnt+=1;
else if(s.equals(""))
continue;
else{
if(cnt>0){
cnt--;
}
else
str = "/"+s+str;
}
}
return str.equals("")? "/" : str;
}