#567
Medium Algorithms Permutation in string
Hash Table Two Pointers String Sliding Window
48.5% acceptance
Jan 13, 2026
12918
519
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
In other words, return true if one of s1's permutations is the substring of s2.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn check_inclusion(s1: String, s2: String) -> bool {
let s1 = s1.as_bytes();
let s2 = s2.as_bytes();
let k = s1.len();
if k > s2.len() { return false; }
let mut need = [0i32; 26];
let mut window = [0i32; 26];
for &b in s1 { need[(b - b'a') as usize] += 1; }
for i in 0..s2.len() {
window[(s2[i] - b'a') as usize] += 1;
if i >= k { window[(s2[i - k] - b'a') as usize] -= 1; }
if window == need { return true; }
}
false
}
}