-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1528_shuffle_string.rs
More file actions
44 lines (39 loc) · 1.1 KB
/
s1528_shuffle_string.rs
File metadata and controls
44 lines (39 loc) · 1.1 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
#![allow(unused)]
pub struct Solution {}
// facebook interview
impl Solution {
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
let chs = s.chars().collect::<Vec<char>>();
let mut ret = vec!['a'; s.len()];
for i in 0..indices.len() {
ret[indices[i] as usize] = chs[i];
}
ret.into_iter().collect::<String>()
}
// cycle sort
pub fn restore_string_cycle_sort(s: String, mut indices: Vec<i32>) -> String {
let mut chs = s.chars().collect::<Vec<char>>();
for i in 0..chs.len() {
while indices[i] as usize != i {
chs.swap(i, indices[i] as usize);
let j = indices[i] as usize;
indices.swap(i, j);
}
}
chs.into_iter().collect::<String>()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1528() {
assert_eq!(
Solution::restore_string_cycle_sort(
"codeleet".to_string(),
vec![4, 5, 6, 7, 0, 2, 1, 3]
),
"leetcode".to_string()
);
}
}