File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
3333- [ 55 Jump Game] ( https://leetcode.com/problems/jump-game/description/ )
3434- [ 56 Merge Intervals] ( https://leetcode.com/problems/merge-intervals/description/ )
3535- [ 57 Insert Interval] ( https://leetcode.com/problems/insert-interval/description/ )
36+ - [ 71 Simplify Path] ( https://leetcode.com/problems/simplify-path/description/ )
3637- [ 80 Remove Duplicates from Sorted Array II] ( https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/ )
3738- [ 88 Merge Sorted Array] ( https://leetcode.com/problems/merge-sorted-array/description/ )
3839- [ 121 Best Time to Buy and Sell Stock] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ )
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def simplifyPath (self , path : str ) -> str :
3+ """
4+ You are given an absolute path for a Unix-style file system,
5+ which always begins with a slash '/'.
6+ Your task is to transform this absolute path
7+ into its simplified canonical path.
8+
9+ The rules of a Unix-style file system are as follows:
10+ - A single period '.' represents the current directory.
11+ - A double period '..' represents the previous/parent directory.
12+ - Multiple consecutive slashes such as '//' and '///' are treated as a single slash '/'.
13+ - Any sequence of periods that does not match the rules above should be
14+ treated as a valid directory or file name.
15+ For example, '...' and '....' are valid directory or file names.
16+
17+ The simplified canonical path should follow these rules:
18+ - The path must start with a single slash '/'.
19+ - Directories within the path must be separated by exactly one slash '/'.
20+ - The path must not end with a slash '/', unless it is the root directory.
21+ - The path must not have any single or double periods ('.' and '..') used
22+ to denote current or parent directories.
23+
24+ Return the simplified canonical path.
25+ """
26+ directories = list (filter (lambda s : s != "" , path .split ("/" )))
27+ stack = []
28+ for d in directories :
29+ if d == "." :
30+ continue
31+ elif d == ".." :
32+ if stack :
33+ stack .pop ()
34+ else :
35+ stack .append (d )
36+ return "/" + "/" .join (stack )
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from awesome_python_leetcode ._71_simplify_path import Solution
4+
5+
6+ @pytest .mark .parametrize (
7+ argnames = ["path" , "expected" ],
8+ argvalues = [
9+ ("/home/" , "/home" ),
10+ ("/home//foo/" , "/home/foo" ),
11+ ("/home/user/Documents/../Pictures" , "/home/user/Pictures" ),
12+ ("/../" , "/" ),
13+ ("/.../a/../b/c/../d/./" , "/.../b/d" ),
14+ ("/a/./b/../../c/" , "/c" ),
15+ ("/a//b////c/d//././/.." , "/a/b/c" ),
16+ ],
17+ )
18+ def test_func (path : str , expected : str ):
19+ simplified_path = Solution ().simplifyPath (path )
20+ assert simplified_path == expected
You can’t perform that action at this time.
0 commit comments