class Solution { public String simplifyPath(String path) { String[] pathSplit = path.split("/"); Deque<String> deque = new LinkedList<>(); for (int i = 0; i < pathSplit.length; i++) { String curStr = pathSplit[i]; if (curStr.equals(".") || curStr.equals("")) { continue; } else if (curStr.equals("..")) { if (!deque.isEmpty()) { deque.pollLast(); } } else { deque.addLast(curStr); } } StringBuilder ans = new StringBuilder("/"); while (!deque.isEmpty()) { ans.append(deque.pollFirst()); if (!deque.isEmpty()) { ans.append("/"); } } return ans.toString().equals("") ? "" : ans.toString(); }}