Skip to content

Commit afba8e2

Browse files
authored
Merge pull request vJechsmayr#191 from rakshaa2000/master
Created simplify path.py
2 parents e1d5021 + f4e1059 commit afba8e2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

LeetCode/0071_Simplify_Path.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def simplifyPath(self, path):
3+
tokens = path.split('/') # some of them could be empty == '//'
4+
stack = []
5+
ans = '/'
6+
for item in tokens:
7+
if item == '':
8+
continue
9+
if item == '.':
10+
pass
11+
elif item == '..':
12+
if len(stack) > 0:
13+
_ = stack.pop()
14+
else:
15+
stack.append(item)
16+
for item in stack:
17+
ans += item
18+
ans += '/'
19+
20+
return ans if len(ans) == 1 else ans[:-1]

0 commit comments

Comments
 (0)