-
Notifications
You must be signed in to change notification settings - Fork 0
/
day255(71)SimplifyPath.cpp
55 lines (54 loc) · 1.48 KB
/
day255(71)SimplifyPath.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
52
53
54
55
class Solution {
public:
string simplifyPath(string path) {
stack<string> s;
int n = path.size();
for(int i=0;i<path.size();i++)
{
if(path[i]=='.')
{
if(i+1<n && path[i+1]!='/')
{
string temp ="";
while(i<n && path[i]!='/')
{
temp+=path[i++];
}
cout<<temp<<endl;
reverse(temp.begin(),temp.end());
if(temp=="..")
{
if(!s.empty()) s.pop();
}else{
s.push(temp);
}
}
}else if(path[i]!='/'){
// int temp = i;
// while(path[i]!='/')
// {
// i++;
// }
// string temp1 = path.substr(temp,i-temp);
// cout<<temp1;
string temp1="";
while(i<n && path[i]!='/')
{
temp1+=path[i++];
}
reverse(temp1.begin(),temp1.end());
s.push(temp1);
}
}
string ans="";
if(s.empty()) ans="/";
while(!s.empty())
{
ans+=s.top();
ans+="/";
s.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};