-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimplifyPath.java
More file actions
42 lines (33 loc) · 1.57 KB
/
Copy pathSimplifyPath.java
File metadata and controls
42 lines (33 loc) · 1.57 KB
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
39
40
41
42
// Given an absolute path for a file (Unix-style), simplify it. Or in other words,
// convert it to the canonical path.
// In a UNIX-style file system, a period . refers to the current directory.
// Furthermore, a double period .. moves the directory up a level.
// Note that the returned canonical path must always begin with a slash /,
// and there must be only a single slash / between two directory names.
// The last directory name (if it exists) must not end with a trailing /.
// Also, the canonical path must be the shortest string representing the absolute path.
// See: https://leetcode.com/problems/simplify-path/
// See: https://leetcode.com/problems/simplify-path/discuss/603200/Java-Slow-but-Concise-Solution
package leetcode.stack;
import java.util.Stack;
public class SimplifyPath {
public String simplifyPath(String path) {
String[] arr = path.replaceAll("/+", "/").split("/");
Stack<String> st = new Stack<>();
for (int i = 1; i < arr.length; i++) {
if (arr[i].equals("..")) {
if (!st.empty())
st.pop();
} else if (!arr[i].equals("."))
st.add(arr[i]);
}
return "/" + String.join("/", st);
}
public static void main(String[] args) {
SimplifyPath sln = new SimplifyPath();
System.out.println(sln.simplifyPath("/"));
System.out.println(sln.simplifyPath("/a//b////c/d//././/.."));
System.out.println(sln.simplifyPath("/a/../../b/../c//.//"));
System.out.println(sln.simplifyPath("/a/./b/../../c/"));
}
}