|
| 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