-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_in_string.rs
More file actions
39 lines (32 loc) · 891 Bytes
/
permutation_in_string.rs
File metadata and controls
39 lines (32 loc) · 891 Bytes
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
pub struct Solution;
impl Solution {
pub fn check_inclusion(s1: String, s2: String) -> bool {
let n = s1.len();
let m = s2.len();
if n == 0 {
return true;
}
if n > m {
return false;
}
let s1_bytes = s1.as_bytes();
let s2_bytes = s2.as_bytes();
let mut s1_count = [0i32; 26];
let mut s2_count = [0i32; 26];
for &b in s1_bytes {
s1_count[(b - b'a') as usize] += 1;
}
for right in 0..m {
let idx = (s2_bytes[right] - b'a') as usize;
s2_count[idx] += 1;
if right >= n {
let left_idx = (s2_bytes[right - n] - b'a') as usize;
s2_count[left_idx] -= 1;
}
if s1_count == s2_count {
return true;
}
}
false
}
}