-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringSplit.java
65 lines (45 loc) · 1.31 KB
/
stringSplit.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
import java.util.Stack;
import java.util.Arrays;
public class stringSplit {
public static void main(String args[]) {
Solution cl = new Solution();
String s= "/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///";
System.out.println(cl.simplifyPath(s));
}
}
class Solution {
public String simplifyPath(String path) {
// Start typing your Java solution below
// DO NOT write main() function
String arr[] = path.split("/");
Stack<String> st = new Stack<String>();
//System.out.println(Arrays.toString(arr) + "\n");
for(String s : arr) {
if (s.equals("."))
continue;
else if (s.equals(""))
continue;
else if (s.equals("..")) {
if (!st.empty())
st.pop();
else
continue;
} else
st.push(s);
}
if (st.empty())
return "/";
String ret = "";
while (!st.empty()) {
String out = st.pop();
//System.out.print(out + " ");
if (out.equals(""))
continue;
out = "/" + out;
ret = out + ret;
}
if (ret.equals(""))
ret = "/";
return ret;
}
}