LC 71 Simplify path

We split the input based on ‘/’.If multiple /// occurs, split will append empty strs in the back.
We will skip . and .. and ” during our iteration through split list and insert other strings in res.
But if we see .., we need to pop one entry from res.

    def simplifyPath(self, path: str) -> str:
        res = []
        skip_set = {'.','..',''}
        for s in path.split('/'):
            if s not in skip_set:        
                res.append(s)
            if s == '..' and res:
                res.pop()
        res_str = ""
        for direc in res:
            res_str += direc + '/'

        return "/" + res_str[:-1]

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax