Skip to content

Commit 6729481

Browse files
committed
p796
1 parent 30bec57 commit 6729481

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/p796.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub fn run() {
2+
for i in [
3+
("abcde".to_string(), "cdeab".to_string()),
4+
("abcde".to_string(), "abced".to_string()),
5+
] {
6+
println!("{}", rotate_string(i.0, i.1));
7+
}
8+
}
9+
10+
pub fn rotate_string(s: String, goal: String) -> bool {
11+
if goal.len() != s.len() {
12+
return false;
13+
}
14+
15+
let goal = goal.chars().collect::<Vec<char>>();
16+
let s = s.chars().collect::<Vec<char>>();
17+
let key = goal[0];
18+
let length = goal.len();
19+
20+
let indices = s.iter().enumerate().filter(|x| *x.1 == key).map(|x| x.0).collect::<Vec<usize>>();
21+
22+
for i in indices {
23+
let mut flag = true;
24+
for n in 0..length {
25+
if s[get(n + i, length)] == goal[n] {
26+
continue;
27+
}
28+
flag = false;
29+
}
30+
if !flag {
31+
continue;
32+
}
33+
return true;
34+
}
35+
36+
false
37+
}
38+
39+
pub fn get(i: usize, length: usize) -> usize {
40+
if i >= length {
41+
return i - length
42+
}
43+
i
44+
}

0 commit comments

Comments
 (0)