All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0501_0600.s0567_permutation_in_string.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0501_0600.s0567_permutation_in_string;

// #Medium #String #Hash_Table #Two_Pointers #Sliding_Window #Algorithm_I_Day_6_Sliding_Window
// #2022_08_10_Time_5_ms_(93.93%)_Space_43.1_MB_(71.37%)

/**
 * 567 - Permutation in String\.
 *
 * Medium
 *
 * 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`.
 *
 * **Example 1:**
 *
 * **Input:** s1 = "ab", s2 = "eidbaooo"
 *
 * **Output:** true
 *
 * **Explanation:** s2 contains one permutation of s1 ("ba").
 *
 * **Example 2:**
 *
 * **Input:** s1 = "ab", s2 = "eidboaoo"
 *
 * **Output:** false
 *
 * **Constraints:**
 *
 * *   1 <= s1.length, s2.length <= 104
 * *   `s1` and `s2` consist of lowercase English letters.
**/
public class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int n = s1.length();
        int m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cntS1 = new int[26];
        int[] cntS2 = new int[26];
        for (int i = 0; i < n; i++) {
            cntS1[s1.charAt(i) - 'a']++;
        }
        for (int i = 0; i < n; i++) {
            cntS2[s2.charAt(i) - 'a']++;
        }
        if (check(cntS1, cntS2)) {
            return true;
        }
        for (int i = n; i < m; i++) {
            cntS2[s2.charAt(i - n) - 'a']--;
            cntS2[s2.charAt(i) - 'a']++;
            if (check(cntS1, cntS2)) {
                return true;
            }
        }
        return false;
    }

    private boolean check(int[] cnt1, int[] cnt2) {
        for (int i = 0; i < 26; i++) {
            if (cnt1[i] != cnt2[i]) {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy