-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Nitish36/main
71.Simplify Path
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.