- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyPath.cpp
More file actions
Latest commit
30 lines (27 loc) · 776 Bytes
/
Copy pathSimplifyPath.cpp
File metadata and controls
30 lines (27 loc) · 776 Bytes
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
classSolution {
public:
string simplifyPath(string path) {
stringstream ss(path);
vector<string> pathStack;
string canonicalPath;
string token;
while(getline(ss, token, '/')){
if (!token.empty() && token != "."){
if (token == ".."){
if(!pathStack.empty()){
pathStack.pop_back();
}
}else{
pathStack.push_back(token);
}
}
}
if (pathStack.empty()){
return"/";
}
for(int i = 0 ; i < pathStack.size(); i++){
canonicalPath += ("/" + pathStack[i]);
}
return canonicalPath;
}
};