Skip to content

Commit 845ee83

Browse files
committed
submit solution of 71. Simplify Path
1 parent ead5005 commit 845ee83

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
class Solution {
6+
func simplifyPath(_ path: String) -> String {
7+
let paths = path.split(separator: "/")
8+
9+
var pathStack = [String]()
10+
11+
for path in paths {
12+
guard path != ".", !path.isEmpty else { continue }
13+
14+
if path == ".." {
15+
pathStack.popLast()
16+
} else {
17+
pathStack.append(String(path))
18+
}
19+
}
20+
21+
return "/" + pathStack.joined(separator: "/")
22+
}
23+
}
24+
25+
// Tests
26+
let s = Solution()
27+
s.simplifyPath("/home/") == "/home"
28+
s.simplifyPath("/../") == "/"
29+
s.simplifyPath("/home//foo/") == "/home/foo"
30+
s.simplifyPath("/a/./b/../../c/") == "/c"
31+
s.simplifyPath("/a/../../b/../c//.//") == "/c"
32+
s.simplifyPath("/a//b////c/d//././/..") == "/a/b/c"
33+
34+
//: [Next](@next)

LeetCodePlayground.playground/contents.xcplayground

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
<page name='75_SortColors'/>
2323
<page name='215_FindKthLargestInAnArray'/>
2424
<page name='88_MergeSortedArray'/>
25+
<page name='71_SimplifyPath'/>
2526
</pages>
2627
</playground>

0 commit comments

Comments
 (0)