String cd(String origin, String path) ;
Input: Origin (current directory), Path(a relative path)Output: Return destination path,
Follow-up: what if path is an absolute path? just ignore origin to process path
//General idea: split the path with "/" and then put them into the stack, when handling the origin path, split it and put it into the stack or choose to pop the content in the stack, finally, give the output.
public static String absolutePath(String origin, String path) {
if (path.length() == 0) return origin;
if (origin.length() == 0) return path;
Stack<String> stack = new Stack<String>();
String[] string = path.split("/");
for (String s : string) {
stack.push(s);
}
String[] str = origin.split("/");
for (String s : str) {
if (s.equals("") || s.equals(".")) continue;
else if (s.equals("..") && !stack.isEmpty()) {
stack.pop();
} else {
stack.push(s);
}
}
StringBuilder result = new StringBuilder();
while (!stack.isEmpty()) {
result.insert(0, stack.pop());
result.insert(0, "/");
}
result.delete(0,1);
return result.toString();
}