rustgym/leetcode/
_1528_shuffle_string.rs

1struct Solution;
2
3impl Solution {
4    fn restore_string(s: String, indices: Vec<i32>) -> String {
5        let n = s.len();
6        let s: Vec<char> = s.chars().collect();
7        let mut v = vec![' '; n];
8        for i in 0..n {
9            v[indices[i] as usize] = s[i];
10        }
11        v.into_iter().collect()
12    }
13}
14
15#[test]
16fn test() {
17    let s = "codeleet".to_string();
18    let indices = vec![4, 5, 6, 7, 0, 2, 1, 3];
19    let res = "leetcode".to_string();
20    assert_eq!(Solution::restore_string(s, indices), res);
21    let s = "abc".to_string();
22    let indices = vec![0, 1, 2];
23    let res = "abc".to_string();
24    assert_eq!(Solution::restore_string(s, indices), res);
25    let s = "aiohn".to_string();
26    let indices = vec![3, 1, 4, 2, 0];
27    let res = "nihao".to_string();
28    assert_eq!(Solution::restore_string(s, indices), res);
29    let s = "aaiougrt".to_string();
30    let indices = vec![4, 0, 2, 6, 7, 3, 1, 5];
31    let res = "arigatou".to_string();
32    assert_eq!(Solution::restore_string(s, indices), res);
33    let s = "art".to_string();
34    let indices = vec![1, 0, 2];
35    let res = "rat".to_string();
36    assert_eq!(Solution::restore_string(s, indices), res);
37}