-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyPath.java
More file actions
134 lines (105 loc) · 3.76 KB
/
SimplifyPath.java
File metadata and controls
134 lines (105 loc) · 3.76 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package Algorithms.StackAlgos;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 17 Oct 2025
* @link 71. Simplify Path <a href="https://leetcode.com/problems/simplify-path/">LeetCode link</a>
* @topics String, Stack
* Description -> Convert "Absolute Path" to "Canonical Path"
*/
public class SimplifyPath {
public static void main(String[] args) {
String path = "/home/////user/Documents/../Pictures";
System.out.println("simplifyPath using stack 1 -> " + simplifyPathUsingStack1(path));
System.out.println("simplifyPath using stack 2 -> " + simplifyPathUsingStack2(path));
System.out.println("simplifyPath using stack 3 -> " + simplifyPathUsingStack3(path));
System.out.println("simplifyPath using deque -> " + simplifyPathUsingDeque(path));
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
public static String simplifyPathUsingStack1(String path) {
Stack<String> stack = new Stack<>();
String[] paths = path.split("/+"); // [, home, user, Documents, .., Pictures]
for (int i=1; i<paths.length; i++) {
String p = paths[i];
if (p.equals("..")) {
if (!stack.isEmpty()) stack.pop();
} else if (!p.equals(".")) {
stack.push(p);
}
}
return "/" + String.join("/", stack);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
public static String simplifyPathUsingStack2(String path) {
Stack<String> stack = new Stack<>();
for (String dir : path.split("/")) { // [, home, , , , , user, Documents, .., Pictures]
if (dir.isEmpty() || dir.equals(".")) {
continue;
} else if (dir.equals("..")) {
if(!stack.isEmpty()) stack.pop();
} else {
stack.push(dir);
}
}
if (stack.isEmpty()) return "/";
StringBuilder sb = new StringBuilder();
for (String dir : stack) {
sb.append("/").append(dir);
}
/*
// or
while (!st.isEmpty()) {
sb.insert(0, "/" + st.pop());
}
*/
return sb.toString();
}
public static String simplifyPathUsingStack3(String path) {
Stack<String> stack = new Stack<>();
StringBuilder cur = new StringBuilder();
// Append '/' at the end to make sure the last component is processed
path = path + "/";
for (char c : path.toCharArray()) {
if (c == '/') {
if (!cur.isEmpty()) {
String dir = cur.toString();
if (dir.equals("..")) {
if (!stack.isEmpty()) stack.pop();
} else if (!dir.equals(".")) {
stack.push(dir);
}
cur.setLength(0); // reset current
}
} else {
cur.append(c);
}
}
return "/" + String.join("/", stack);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
public static String simplifyPathUsingDeque(String path) {
Deque<String> deque = new ArrayDeque<>();
String[] segments = path.split("/");
for (String segment : segments) {
if (segment.equals("..")) {
if (!deque.isEmpty()) {
deque.removeLast(); // pop from stack
}
} else if (segment.isEmpty() || segment.equals(".")) { // skip empty or current directory
continue;
} else {
deque.addLast(segment); // push to stack
}
}
return "/" + String.join("/", deque);
}
}