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.
30:00
Java
Permutation in String
medium
Topics
Companies
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
Constraints:
1≤s1.length,s2.length≤104
s1 and s2 consist of lowercase English letters.
Input
arr ={"s1":"ab","s2":"eidbaooo"}
s2 = "eidbaooo"
e
i
d
b
a
o
o
o
0
1
2
3
4
5
6
7
Window:
[0...1]
Matches:
0
s1 "ab" character count
Empty
Current window character count
Empty
Algorithm Insight:
A permutation has the same character frequencies. We slide a window of size |s1| over s2, checking if the window's character count matches s1's count.