Sponsored
Sponsored
The main idea is to use a sliding window technique to compare the character frequency of a substring of s2 with the character frequency of s1. If they match, it means s2 contains a permutation of s1.
Time Complexity: O(n), where n is the length of s2, as each character in s2 is processed once.
Space Complexity: O(1), as the frequency arrays have a fixed size of 26.
1import java.util.Arrays;
2
3public class Solution {
4 public boolean checkInclusion(String s1, String s2) {
5
This Java solution leverages the Arrays.equals method to compare two frequency count arrays, applying the sliding window technique to check for a permutation.
Leverage a hash map (or dictionary) to record the character count of s1 and use it to compare with segments of s2. This approach focuses on incremental updates to the map as the window slides over s2.
Time Complexity: O(n), where n is the length of s2.
Space Complexity: O(1), since the hash map's size is bounded by the character set size of 26.
1var checkInclusion = function(s1,
This JavaScript solution adopts a similar strategy to the Python solution using objects to store character counts and keeps a running number of matched frequencies, sliding the window across s2.