Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Renat Zamaletdinov
I didn't pay enough attention to the description so I though it's required to use only stack operations, without any splitting processing char by char

Renat Zamaletdinov

Feb 15, 2024

So I ended up with that awful, yet working solution

class Solution:     def simplifyPath(self, path):         stack = []         l = len(path)         i = 0         while i < l:             char = path[i]             if char == '/':                 while stack and stack[-1] == '/':                     stack.pop()             elif char == '.':                 if i < l - 1 and path[i + 1] == '.':                     if stack and stack[-1] == '/':                         stack.pop()                     while stack and stack[-1] != '/':                         stack.pop()             if char != '.':                 stack.append(char)             i += 1         while stack and stack[-1] == '/':             stack.pop()         if not stack:             return '/'         return ''.join(stack)

3

0

Comments
Comments

On this page