-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathSimplifyPath.java
108 lines (93 loc) · 2.77 KB
/
SimplifyPath.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
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
// @saorav21994
// TC : O(n)
// SC : O(n)
class Solution {
public String simplifyPath(String path) {
path = path.replaceAll("//+", "/");
// System.out.println(path);
String [] levels = path.split("/");
Deque<String> st = new ArrayDeque<>();
// System.out.println("print string array");
// for (String s : levels) {
// System.out.println(s + "-");
// }
for (String s : levels) {
// System.out.println(s);
if (!s.equals("..") && !s.equals(".")) {
st.offer(s);
}
else if (s.equals("..") && !st.isEmpty()) {
st.removeLast();
}
}
StringBuilder sb = new StringBuilder();
while (!st.isEmpty()) {
// System.out.println(st.peek());
sb.append(st.poll());
sb.append("/");
// System.out.println(sb);
}
if (sb.length() == 0 || sb.charAt(0) != '/')
sb.insert(0, "/");
String res = sb.toString();
if (res.length() == 1)
return res;
else
return res.substring(0, res.length()-1);
}
}
// Author: @romitdutta10
// TC : O(N)
// SC: O(N)
// Problem : https://leetcode.com/problems/simplify-path/
// Solution without using stack
class Solution {
public String simplifyPath(String path) {
String paths[] = path.split("/");
StringBuilder res = new StringBuilder();
for(String p : paths) {
if(p.length() <= 0 || p.equals(".")) {
continue;
}
if(p.equals("..")) {
if(res.length() > 0) {
res.setLength(res.lastIndexOf("/"));
}
} else {
res.append("/").append(p);
}
}
return res.length() == 0 ? "/" : res.toString();
}
}
// Author: @romitdutta10
// TC : O(N)
// SC: O(N)
// Problem : https://leetcode.com/problems/simplify-path/
// Solution with using stack
class Solution {
public String simplifyPath(String path) {
Stack<String> st = new Stack<>();
String[] pathList = path.split("\\/");
for(String p : pathList){
if(p.length() == 0 || p.equals(".")){
continue;
} else if(p.equals("..")){
if(!st.empty()){
st.pop();
}
} else {
st.push(p);
}
}
StringBuilder ans = new StringBuilder();
while(!st.empty()){
ans.insert(0, st.pop() + "/");
}
if(ans.length() == 0){
return "/";
}else{
return "/" + ans.substring(0, ans.length() -1);
}
}
}