Permutation in String - Problem
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.
A permutation is a rearrangement of characters. For example, "abc" has permutations "abc", "acb", "bac", "bca", "cab", and "cba".
Input & Output
Example 1 — Basic Permutation Found
$
Input:
s1 = "ab", s2 = "eidbaooo"
›
Output:
true
💡 Note:
s2 contains "ba" which is a permutation of "ab" (same characters: a=1, b=1)
Example 2 — No Permutation
$
Input:
s1 = "ab", s2 = "eidboaoo"
›
Output:
false
💡 Note:
No substring of s2 with length 2 has the same character frequencies as "ab"
Example 3 — Exact Match
$
Input:
s1 = "adc", s2 = "dcda"
›
Output:
true
💡 Note:
s2 contains "dcd" which has characters d=2, c=1 but "adc" has a=1, d=1, c=1. Actually "cda" has c=1, d=1, a=1 which matches "adc"
Constraints
- 1 ≤ s1.length, s2.length ≤ 104
- s1 and s2 consist of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input
s1="ab" (need to find permutation), s2="eidbaooo" (search space)
2
Process
Check character frequencies of substrings
3
Output
Found "ba" which has same frequencies as "ab"
Key Takeaway
🎯 Key Insight: Instead of generating permutations, compare character frequency counts using sliding window
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code