-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSimplifyDirectory.java
More file actions
55 lines (47 loc) · 1.29 KB
/
SimplifyDirectory.java
File metadata and controls
55 lines (47 loc) · 1.29 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
package Stacks;
import java.util.Stack;
/**
* Author - archit.s
* Date - 22/10/18
* Time - 11:30 PM
*/
public class SimplifyDirectory {
public String simplifyPath(String A) {
Stack<String> s = new Stack<>();
for(int i=0;i<A.length();){
StringBuilder temp = new StringBuilder();
if(A.charAt(i) == '/'){
i++;
continue;
}
else if(A.charAt(i) =='.'){
if(i+1<A.length()){
if(A.charAt(i+1) == '.' && !s.empty()){
s.pop();
i++;
}
}
i++;
}
else{
while(i<A.length() && A.charAt(i) !='/'){
temp.append(A.charAt(i));
i++;
}
s.push(temp.toString());
}
}
StringBuilder ans = new StringBuilder();
while(!s.empty()){
ans.insert(0, "/" + s.peek());
s.pop();
}
if(ans.toString().equals("")){
return "/";
}
return ans.toString();
}
public static void main(String[] args) {
System.out.println(new SimplifyDirectory().simplifyPath("/home//foo/"));
}
}