Skip to content

Commit a939e8b

Browse files
committed
Added solution of problem 1528
1 parent 35ef313 commit a939e8b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ pub mod s1507_reformat_date;
319319
pub mod s1512_number_of_good_pairs;
320320
pub mod s1518_water_bottles;
321321
pub mod s1523_count_odd_numbers_in_an_interval_range;
322+
pub mod s1528_shuffle_string;
322323
pub mod s1550_three_consecutive_odds;
323324
pub mod s1619_mean_of_array_after_removing_some_elements;
324325
pub mod s1935_maximum_number_of_words_you_can_type;

src/solutions/s1528_shuffle_string.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
2+
let mut shuffled_s: Vec<char> = vec![' '; s.len()];
3+
4+
s.chars().zip(indices).for_each(|(c, i)| {
5+
shuffled_s[i as usize] = c;
6+
});
7+
8+
shuffled_s.into_iter().collect()
9+
}
10+
11+
#[cfg(test)]
12+
mod test {
13+
use super::*;
14+
15+
#[test]
16+
fn test_case_1() {
17+
assert_eq!(
18+
restore_string("codeleet".to_string(), vec![4, 5, 6, 7, 0, 2, 1, 3]),
19+
"leetcode"
20+
);
21+
}
22+
23+
#[test]
24+
fn test_case_2() {
25+
assert_eq!(restore_string("abc".to_string(), vec![0, 1, 2]), "abc");
26+
}
27+
}

0 commit comments

Comments
 (0)