You are given two strings s1 and s2 made of lowercase English letters. Return true if s2 contains a permutation of s1 as a substring, and false otherwise.
A permutation of s1 is a string made of the same letters as s1, each one as many times, in any order: "abb", "bab" and "bba" are the permutations of "abb". A substring is a run of consecutive characters of s2.
So the question is: does s2 have a run of consecutive letters, exactly as long as s1, that holds every letter as many times as s1 does?
Your solution returns a boolean, and the tests print 1 for true and 0 for false.
Example 1:
Input: s1 = "ab", s2 = "eidbaooo" Output: true Explanation: s2 contains "ba", at indexes 3 and 4, and "ba" is a permutation of "ab".
Example 2:
Input: s1 = "ab", s2 = "eidboaoo" Output: false Explanation: s2 has an a and a b, but never next to each other: every run of two letters misses one of them.
Example 3:
Input: s1 = "adc", s2 = "dcda" Output: true Explanation: The last three letters, "cda", hold one a, one c and one d, just like "adc". The first three, "dcd", have two d's and no a.
Constraints:
1 <= s1.length, s2.length <= 105s1ands2are made of lowercase English letters only (a to z).
Note:
Your algorithm should run in O(n + m) time, where n is the length of s2 and m the length of s1, and use O(1) extra space: one count for each of the 26 letters. Checking every substring of s2 on its own takes O(n · m) time, too slow for the largest tests.
Free plan: 30 tutor messages. PRO is unlimited.
