Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.19 KB

_71. Simplify Path.md

File metadata and controls

53 lines (38 loc) · 1.19 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 11, 2024

Last updated : July 01, 2024


Related Topics : String, Stack

Acceptance Rate : 45.57 %


Solutions

Python

class Solution:
    def simplifyPath(self, path: str) -> str:
        output = deque()

        temp = path.replace('//', '/')

        while '//' in temp :
            temp = temp.replace('//', '/')
        temp = temp.split('/')

        for folder in temp :
            if folder == '.' :
                continue
            if folder == '..' :
                if len(output) > 0 :
                    output.pop()
                continue
            output.append(folder)
        
        while len(output) > 0 and output[0] == '' :
            output.popleft()
        while len(output) > 0 and output[-1] == '' :
            output.pop()

        return '/' + '/'.join(output)