- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind-and-replace-in-string.rs
40 lines (32 loc) · 1.09 KB
/
find-and-replace-in-string.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fnmain(){}
structSolution;
implSolution{
pubfnfind_replace_string(
s:String,
indices:Vec<i32>,
sources:Vec<String>,
targets:Vec<String>,
) -> String{
letmut map1: std::collections::HashMap<_,_> = std::collections::HashMap::new();
letmut map2: std::collections::HashMap<_,_> = std::collections::HashMap::new();
for i in0..indices.len(){
map1.insert(indices[i]asusize,&sources[i]);
map2.insert(indices[i]asusize, i);
}
letmut result = Vec::new();
letmut i = 0;
while i < s.len(){
ifletSome(x) = map1.get(&i){
if s[i..].starts_with(x.as_str()){
result.extend_from_slice(targets[*map2.get(&i).unwrap()].as_bytes());
i += x.len();
continue;
}
}
result.push(s.as_bytes()[i]);
i += 1;
}
unsafe{String::from_utf8_unchecked(result)}
}
}