forked from sastava007/Tech-Interview-Preparation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify Path.cpp
More file actions
35 lines (30 loc) · 973 Bytes
/
Simplify Path.cpp
File metadata and controls
35 lines (30 loc) · 973 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
31
32
33
34
35
/* Parse the path to obtain token, and make sure few things
1. / means root
2. .. means going upward in directry, so remove the top most element from stack. And if already present at top then stay there
3. /.dir is valid directry name, becz folders can contain '.'
TC: O(N)
Input: "/a/./b/../../c/" Output: "/c"
Input: "/a//b////c/d//././/.." Output: "/a/b/c"
*/
class Solution {
public:
string simplifyPath(string path)
{
vector<string> stk;
stringstream ss(path);
string token;
while(getline(ss, token, '/'))
{
if(token=="" || token==".")
continue;
else if(token != "..")
stk.emplace_back(token);
else if(!stk.empty())
stk.pop_back();
}
string res="";
for(string &token: stk)
res += "/" + token;
return stk.empty()? "/":res;
}
};