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