Simple regex solution (52ms).

  • 0

    Simple regular expression solution in 52ms.
    Use findall to get result as split.
    Add '/' before path for case like './test'

    import re
    class Solution(object):
        def simplifyPath(self, path):
            path = '/' + path
            result = []
            for p in re.findall('/([^/]*)', path):
                if p == "..":
                    if len(result) > 0:
                elif p and p != '.':
            return '/' + '/'.join(result)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.