Interview 150
Simplify Path
- Problem
- LC 71
- File
- interview150_LC71SimplifyPath.java
- Path
- pkg5leetcode/interview150/interview150_LC71SimplifyPath.java
- Package
- pkg5leetcode.interview150
- Command
- java pkg5leetcode/interview150/interview150_LC71SimplifyPath.java
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg5leetcode.interview150;2 3/** LC 71 Simplify Path */4import java.util.*;5 6public class interview150_LC71SimplifyPath {7 static String simplifyPath(String path) {8 Deque<String> st = new ArrayDeque<>();9 for (String p : path.split("/")) {10 if (p.isEmpty() || p.equals(".")) continue;11 if (p.equals("..")) { if (!st.isEmpty()) st.pollLast(); }12 else st.addLast(p);13 }14 return "/" + String.join("/", st);15 }16 17 public static void main(String[] args) {18 System.out.println(simplifyPath("/home/"));19 System.out.println(simplifyPath("/../"));20 System.out.println(simplifyPath("/home//foo/"));21 }22}