Skip to content

Commit

Permalink
Merge pull request #43 from Nitish36/main
Browse files Browse the repository at this point in the history
71.Simplify Path
  • Loading branch information
iamdestinychild authored Oct 3, 2023
2 parents 4d71f6c + c450273 commit 5c08179
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
39 changes: 39 additions & 0 deletions LeetCode/71.Simplify Path/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 71. Simplify Path

Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

The canonical path should have the following format:

1)The path starts with a single slash '/'.
2)Any two directories are separated by a single slash '/'.
3)The path does not end with a trailing '/'.
4)The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
5)Return the simplified canonical path.

## Examples

### Example 1:

Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

### Example 2:

Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.

### Example 3:

Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

## Constraints

1 <= path.length <= 3000
path consists of English letters, digits, period '.', slash '/' or '_'.
path is a valid absolute Unix path.
39 changes: 39 additions & 0 deletions LeetCode/71.Simplify Path/python/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 71. Simplify Path (https://leetcode.com/problems/simplify-path/description/)

Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

The canonical path should have the following format:

The path starts with a single slash '/'.
Any two directories are separated by a single slash '/'.
The path does not end with a trailing '/'.
The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
Return the simplified canonical path.

## Examples

### Example 1:

Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

### Example 2:

Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.

### Example 3:

Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

## Constraints

1 <= path.length <= 3000
path consists of English letters, digits, period '.', slash '/' or '_'.
path is a valid absolute Unix path.
5 changes: 5 additions & 0 deletions LeetCode/71.Simplify Path/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This code was done using python

To run the above code in terminal type the below code

python main.py
22 changes: 22 additions & 0 deletions LeetCode/71.Simplify Path/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
for dir in path.split('/'):
if dir == '' or dir == '.':
continue
elif dir == '..':
if stack:
stack.pop()
else:
stack.append(dir)

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

def main():
solution = Solution()
path = "/home//foo/" #Example case hardcoded
soln = solution.simplifyPath(path)
print(soln)

if __name__ =="__main__":
main()
Binary file added LeetCode/71.Simplify Path/python/snapshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c08179

Please sign in to comment.