-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt53.cpp
51 lines (50 loc) · 1.32 KB
/
lt53.cpp
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
class Solution {
public:
string simplifyPath(string path) {
int n=path.size();
stack<string> seedha;
string temp;
for(int i=0 ; i<n ; i++){
while(i<n && path[i]!='/'){
temp.push_back(path[i]);
i++;
}
if(!temp.empty()){
seedha.push(temp);
temp.clear();
}
}
stack<string> reverse;
while(!seedha.empty()){
reverse.push(seedha.top());
seedha.pop();
}
stack<string> last;
string result;
while(!reverse.empty()){
if(reverse.top()=="."){
reverse.pop();
continue;
}
if(reverse.top()==".."){
if(!last.empty()) last.pop();
reverse.pop();
continue;
}
last.push(reverse.top());
reverse.pop();
}
stack<string> lastreverse;
while(!last.empty()){
lastreverse.push(last.top());
last.pop();
}
if(lastreverse.empty()) return "/";
while(!lastreverse.empty()){
result.push_back('/');
result.append(lastreverse.top());
lastreverse.pop();
}
return result;
}
};