Skip to content

Commit 62c0f07

Browse files
committed
LC 2000. Reverse Prefix of Word (Rust)
1 parent b2095f5 commit 62c0f07

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
588588
| [1980. Find Unique Binary String][lc1980] | 🟠 Medium | [![rust](res/rs.png)][lc1980rs] |
589589
| [1991. Find the Middle Index in Array][lc1991] | 🟢 Easy | [![python](res/py.png)][lc1991py] |
590590
| [1996. The Number of Weak Characters in the Game][lc1996] | 🟠 Medium | [![python](res/py.png)][lc1996py] |
591+
| [2000. Reverse Prefix of Word][lc2000] | 🟢 Easy | [![rust](res/rs.png)][lc2000rs] |
591592
| [2007. Find Original Array From Doubled Array][lc2007] | 🟠 Medium | [![python](res/py.png)][lc2007py] |
592593
| [2009. Minimum Number of Operations to Make Array Continuous][lc2009] | 🔴 Hard | [![rust](res/rs.png)][lc2009rs] |
593594
| [2013. Detect Squares][lc2013] | 🟠 Medium | [![python](res/py.png)][lc2013py] |
@@ -2003,6 +2004,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
20032004
[lc1991py]: leetcode/find-the-middle-index-in-array.py
20042005
[lc1996]: https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
20052006
[lc1996py]: leetcode/the-number-of-weak-characters-in-the-game.py
2007+
[lc2000]: https://leetcode.com/problems/reverse-prefix-of-word/
2008+
[lc2000rs]: leetcode/reverse-prefix-of-word.rs
20062009
[lc2007]: https://leetcode.com/problems/find-original-array-from-doubled-array/
20072010
[lc2007py]: leetcode/find-original-array-from-doubled-array.py
20082011
[lc2009]: https://leetcode.com/problems/minimum-number-of-operations-to-make-array-continuous/

leetcode/reverse-prefix-of-word.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// 2000. Reverse Prefix of Word
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/reverse-prefix-of-word/
5+
//
6+
// Tags: Two Pointers - String
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Check if the character is found in the input, if found reverse the characters up to and
11+
/// including that index, then append the remaining characters and return that.
12+
///
13+
/// Time complexity: O(n) - We iterate over the characters once to find the first index of, if
14+
/// found, we iterate in reverse over the characters up to that index then forward over the
15+
/// characters after that index, building the result.
16+
/// Space complexity: O(1) - Unless iterators are internally using extra memory, which I don't
17+
/// think is the case, then constant extra memory.
18+
///
19+
/// Runtime 0 ms Beats 100%
20+
/// Memory 2.07 MB Beats 90%
21+
pub fn reverse_prefix(word: String, ch: char) -> String {
22+
match word.chars().position(|c| c == ch) {
23+
Some(i) => word[..=i]
24+
.chars()
25+
.rev()
26+
.chain(word[i + 1..].chars())
27+
.collect(),
28+
None => word,
29+
}
30+
}
31+
}
32+
33+
// Tests.
34+
fn main() {
35+
let tests = [
36+
("abcdefd", 'd', "dcbaefd"),
37+
("xyxzxe", 'z', "zxyxxe"),
38+
("abcd", 'z', "abcd"),
39+
];
40+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
41+
let mut success = 0;
42+
for (i, t) in tests.iter().enumerate() {
43+
let res = Solution::reverse_prefix(t.0.to_string(), t.1);
44+
if res == t.2 {
45+
success += 1;
46+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
47+
} else {
48+
println!(
49+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
50+
i, t.2, res
51+
);
52+
}
53+
}
54+
println!();
55+
if success == tests.len() {
56+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
57+
} else if success == 0 {
58+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
59+
} else {
60+
println!(
61+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
62+
tests.len() - success
63+
)
64+
}
65+
}

0 commit comments

Comments
 (0)