-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0796_rotate_string.rs
More file actions
49 lines (43 loc) · 1.14 KB
/
s0796_rotate_string.rs
File metadata and controls
49 lines (43 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![allow(unused)]
pub struct Solution {}
// microsoft interview
impl Solution {
pub fn rotate_string(a: String, b: String) -> bool {
let (cha, chb) = (
a.chars().collect::<Vec<char>>(),
b.chars().collect::<Vec<char>>(),
);
// a len not equal to b , can't shift
if cha.len() != chb.len() {
return false;
}
if cha.len() == 0 {
return true;
}
for i in 0..chb.len() {
// i represent partition index
if chb[i] != cha[0] {
continue;
}
let mut j = i + 1;
while j % chb.len() != i {
// b string compare failed a string, jump out loop
if chb[j % chb.len()] != cha[j - i] {
break;
}
j += 1;
}
// if b string compare success a string, run all the loop ,and j % chb.len() == i
if j % chb.len() == i {
return true;
}
}
return false;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_796() {}
}