File tree 1 file changed +16
-15
lines changed
scripts/algorithms/S/Simplify Path
1 file changed +16
-15
lines changed Original file line number Diff line number Diff line change 1
- // 😉😉😉😉Please upvote if it helps 😉😉😉😉
1
+ // Runtime: 10 ms (Top 59.80%) | Memory: 10.6 MB (Top 29.82%)
2
+ // Please upvote if it helps
2
3
class Solution {
3
4
public:
4
5
string simplifyPath (string path) {
5
-
6
+
6
7
stack<string> st;
7
8
string res;
8
-
9
- for (int i = 0 ; i<path.size (); ++i)
9
+
10
+ for (int i = 0 ; i<path.size (); ++i)
10
11
{
11
- if (path[i] == ' /' )
12
+ if (path[i] == ' /' )
12
13
continue ;
13
14
string temp;
14
- // iterate till we doesn't traverse the whole string and doesn't encounter the last /
15
+ // iterate till we doesn't traverse the whole string and doesn't encounter the last /
15
16
while (i < path.size () && path[i] != ' /' )
16
17
{
17
- // add path to temp string
18
+ // add path to temp string
18
19
temp += path[i];
19
20
++i;
20
21
}
21
22
if (temp == " ." )
22
23
continue ;
23
- // pop the top element from stack if exists
24
+ // pop the top element from stack if exists
24
25
else if (temp == " .." )
25
26
{
26
27
if (!st.empty ())
27
28
st.pop ();
28
29
}
29
30
else
30
- // push the directory file name to stack
31
+ // push the directory file name to stack
31
32
st.push (temp);
32
33
}
33
-
34
- // adding all the stack elements to res
34
+
35
+ // adding all the stack elements to res
35
36
while (!st.empty ())
36
37
{
37
38
res = " /" + st.top () + res;
38
39
st.pop ();
39
40
}
40
-
41
- // if no directory or file is present
41
+
42
+ // if no directory or file is present
42
43
if (res.size () == 0 )
43
44
return " /" ;
44
-
45
+
45
46
return res;
46
47
}
47
- };
48
+ };
You can’t perform that action at this time.
0 commit comments