Skip to content

Commit 457b3d3

Browse files
committed
LC 71. Simplify Path (Rust Stack)
1 parent 967bb3f commit 457b3d3

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
7474
| [66. Plus One][lc66] | 🟢 Easy | [![python](res/py.png)][lc66py] |
7575
| [67. Add Binary][lc67] | 🟢 Easy | [![python](res/py.png)][lc67py] [![rust](res/rs.png)][lc67rs] |
7676
| [70. Climbing Stairs][lc70] | 🟢 Easy | [![python](res/py.png)][lc70py] |
77-
| [71. Simplify Path][lc71] | 🟠 Medium | [![python](res/py.png)][lc71py] |
77+
| [71. Simplify Path][lc71] | 🟠 Medium | [![python](res/py.png)][lc71py] [![rust](res/rs.png)][lc71rs] |
7878
| [72. Edit Distance][lc72] | 🔴 Hard | [![python](res/py.png)][lc72py] [![rust](res/rs.png)][lc72rs] |
7979
| [73. Set Matrix Zeroes][lc73] | 🟠 Medium | [![python](res/py.png)][lc73py] |
8080
| [74. Search a 2D Matrix][lc74] | 🟠 Medium | [![python](res/py.png)][lc74py] |
@@ -599,6 +599,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
599599
[lc70py]: leetcode/climbing-stairs.py
600600
[lc71]: https://leetcode.com/problems/simplify-path/
601601
[lc71py]: leetcode/simplify-path.py
602+
[lc71rs]: leetcode/simplify-path.rs
602603
[lc72]: https://leetcode.com/problems/edit-distance/
603604
[lc72py]: leetcode/edit-distance.py
604605
[lc72rs]: leetcode/edit-distance.rs

leetcode/simplify-path.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// 71. Simplify Path
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/simplify-path/
5+
//
6+
// Tags: String - Stack
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Use a stack to collect the directories that form the path, iterate over
11+
/// the tokens in the input, skip "." and empty tokens, pop the last
12+
/// directory when we see ".." and push anything else. Use the contents of
13+
/// the stack to build the resulting simplified path.
14+
///
15+
/// Time complexity: O(n) - We visit all characters in the input string and
16+
/// do O(1) work for each.
17+
/// Space complexity: O(n) - The stack grows in size linearly with the input.
18+
///
19+
/// Runtime 0 ms Beats 100%
20+
/// Memory 2.2 MB Beats 42.22%
21+
pub fn simplify_path(path: String) -> String {
22+
let mut dirs = vec![];
23+
for token in path.split('/') {
24+
match token {
25+
"." | "" => continue,
26+
".." => {
27+
dirs.pop();
28+
}
29+
_ => dirs.push(token),
30+
}
31+
}
32+
format!("{}{}", "/", dirs.join("/"))
33+
}
34+
35+
/// Functional programming version of the solution, same logic as the
36+
/// previous solution but uses fold to build the result path.
37+
///
38+
/// Time complexity: O(n) - We visit all characters in the input string and
39+
/// do O(1) work for each.
40+
/// Space complexity: O(n) - The stack grows in size linearly with the input.
41+
///
42+
/// Runtime 2 ms Beats 44.44%
43+
/// Memory 2.2 MB Beats 73.33%
44+
pub fn simplify_path_2(path: String) -> String {
45+
format!(
46+
"{}{}",
47+
"/",
48+
path.split('/')
49+
.fold(vec![], |mut v, d| match d {
50+
"." | "" => v,
51+
".." => {
52+
v.pop();
53+
v
54+
}
55+
_ => {
56+
v.push(d);
57+
v
58+
}
59+
})
60+
.join("/")
61+
)
62+
}
63+
}
64+
65+
// Tests.
66+
fn main() {
67+
let tests = [
68+
["/../", "/"],
69+
["/a/..", "/"],
70+
["/a/../", "/"],
71+
["/home/", "/home"],
72+
["/abc/...", "/abc/..."],
73+
["/a/./b/../../c/", "/c"],
74+
["/a/../.././../../", "/"],
75+
["/../../../../../a", "/a"],
76+
["/home//foo/", "/home/foo"],
77+
["/a//b//c//////d", "/a/b/c/d"],
78+
["/a/./b/./c/./d/", "/a/b/c/d"],
79+
];
80+
for t in tests {
81+
assert_eq!(
82+
Solution::simplify_path(String::from(t[0])),
83+
String::from(t[1])
84+
);
85+
assert_eq!(
86+
Solution::simplify_path_2(String::from(t[0])),
87+
String::from(t[1])
88+
);
89+
}
90+
println!("\x1b[92m» All tests passed!\x1b[0m")
91+
}

0 commit comments

Comments
 (0)