forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (35 loc) · 1.14 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package g0001_0100.s0071_simplify_path;
// #Medium #String #Stack #Top_Interview_150_Stack
// #2025_03_05_Time_2_ms_(99.86%)_Space_43.12_MB_(91.80%)
import java.util.ArrayDeque;
import java.util.Deque;
public class Solution {
public String simplifyPath(String path) {
Deque<String> stk = new ArrayDeque<>();
int start = 0;
while (start < path.length()) {
while (start < path.length() && path.charAt(start) == '/') {
start++;
}
int end = start;
while (end < path.length() && path.charAt(end) != '/') {
end++;
}
String s = path.substring(start, end);
if (s.equals("..")) {
if (!stk.isEmpty()) {
stk.pop();
}
} else if (!s.equals(".") && !s.equals("")) {
stk.push(s);
}
start = end + 1;
}
StringBuilder ans = new StringBuilder();
while (!stk.isEmpty()) {
ans.insert(0, stk.pop());
ans.insert(0, "/");
}
return ans.length() > 0 ? ans.toString() : "/";
}
}