Skip to content

Commit 1d9c3b1

Browse files
committed
LC 796. Rotate String (Rust)
1 parent 72973f9 commit 1d9c3b1

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ to the solution in this repository.
374374
| [790. Domino and Tromino Tiling][lc790] | 🟠 Medium | [![python](res/py.png)][lc790py] |
375375
| [791. Custom Sort String][lc791] | 🟠 Medium | [![rust](res/rs.png)][lc791rs] |
376376
| [792. Number of Matching Subsequences][lc792] | 🟠 Medium | [![python](res/py.png)][lc792py] |
377+
| [796. Rotate String][lc796] | 🟢 Easy | [![rust](res/rs.png)][lc796rs] |
377378
| [797. All Paths From Source to Target][lc797] | 🟠 Medium | [![python](res/py.png)][lc797py] |
378379
| [799. Champagne Tower][lc799] | 🟠 Medium | [![rust](res/rs.png)][lc799rs] |
379380
| [804. Unique Morse Code Words][lc804] | 🟢 Easy | [![python](res/py.png)][lc804py] |
@@ -1600,6 +1601,8 @@ to the solution in this repository.
16001601
[lc791rs]: leetcode/custom-sort-string.rs
16011602
[lc792]: https://leetcode.com/problems/number-of-matching-subsequences/
16021603
[lc792py]: leetcode/number-of-matching-subsequences.py
1604+
[lc796]: https://leetcode.com/problems/rotate-string/
1605+
[lc796rs]: leetcode/rotate-string.rs
16031606
[lc797]: https://leetcode.com/problems/all-paths-from-source-to-target/
16041607
[lc797py]: leetcode/all-paths-from-source-to-target.py
16051608
[lc799]: https://leetcode.com/problems/champagne-tower/

leetcode/rotate-string.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// 796. Rotate String
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/rotate-string/
5+
//
6+
// Tags: String - String Matching
7+
8+
struct Solution;
9+
impl Solution {
10+
/// The easiest solution is to concatenate one of the strings and check if the other is a
11+
/// substring of the concatenation.
12+
///
13+
/// Time complexity: O(n^2) - The nested loop.
14+
/// Space complexity: O(n) - The char vec.
15+
///
16+
/// Runtime 0 ms Beats 100%
17+
/// Memory 2.21 MB Beats 26%
18+
#[allow(dead_code)]
19+
pub fn rotate_string_nsqr(s: String, goal: String) -> bool {
20+
let (m, n) = (s.len(), goal.len());
21+
if m != n {
22+
return false;
23+
}
24+
let goal = goal.chars().chain(goal.chars()).collect::<Vec<_>>();
25+
let first_char = s.chars().next().unwrap();
26+
for idx in 0..n {
27+
if goal[idx] != first_char {
28+
continue;
29+
}
30+
// Current character matches the first character, try a match.
31+
let mut is_match = true;
32+
for (i, c) in s.chars().enumerate() {
33+
if c != goal[idx + i] {
34+
is_match = false;
35+
break;
36+
}
37+
}
38+
if is_match {
39+
return true;
40+
}
41+
}
42+
false
43+
}
44+
45+
/// The same solution is much cleaner using built-in functions, probably faster as well.
46+
///
47+
/// Time complexity: O(n^2?) - String::contains internally uses std::find() which uses loops
48+
/// just like the previous solution does, they do remark that most times it will run in O(n)
49+
/// Space complexity: O(n) - Repeat creates a new string.
50+
///
51+
/// Runtime 0 ms Beats 100%
52+
/// Memory 2.11 MB Beats 26%
53+
#[allow(dead_code)]
54+
pub fn rotate_string(s: String, goal: String) -> bool {
55+
s.len() == goal.len() && goal.repeat(2).contains(&s)
56+
}
57+
}
58+
59+
// Tests.
60+
fn main() {
61+
let tests = [("abcde", "cdeab", true), ("abcde", "abced", false)];
62+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
63+
let mut success = 0;
64+
for (i, t) in tests.iter().enumerate() {
65+
let res = Solution::rotate_string(t.0.to_owned(), t.1.to_owned());
66+
if res == t.2 {
67+
success += 1;
68+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
69+
} else {
70+
println!(
71+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
72+
i, t.1, res
73+
);
74+
}
75+
}
76+
println!();
77+
if success == tests.len() {
78+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
79+
} else if success == 0 {
80+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
81+
} else {
82+
println!(
83+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
84+
tests.len() - success
85+
)
86+
}
87+
}

0 commit comments

Comments
 (0)