Approach Summary
Slide a window of length p.length across s. At each step compare frequency maps; collect starting indices where they match.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Solution Progression (Brute Force → Most Optimized)
Understand the architectural evolution from naive baseline to optimal complexity.
| Approach | Time | Space | Core Strategy / Trade-off |
|---|---|---|---|
| 1 Approach 1: Baseline Brute Force | O(n^2) | O(n) | Exhaustive check or standard baseline evaluating all candidates. |
| 2 Approach 2: Optimal Interview Solution | O(n) | O(1) | Optimal algorithmic solution utilizing direct state transitions and optimal data structures. |
Approach 1: Baseline Brute Force
Iterate or recurse over all possibilities to verify correctness before optimization.
Approach 1: Baseline Brute Force Implementation
Approach 1: Baseline Brute Force
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList<>();
if(s == null || p == null || s.length() < p.length()){
return result;
}
int sCount[] = new int[26];
int pCount[] = new int[26];
int k = p.length();
for(int i=0; i<k; i++){
pCount[p.charAt(i) - 'a']++;
}
for(int i=0; i<k; i++){
sCount[s.charAt(i) - 'a']++;
}
if(Arrays.equals(sCount, pCount)){
result.add(0);
}
int left = 0;
for(int right=k; right<s.length(); right++){
sCount[s.charAt(left) - 'a']--;
left++;
sCount[s.charAt(right) - 'a']++;
if(Arrays.equals(sCount, pCount)){
result.add(left);
}
}
return result;
}
}class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> result;
if(s.empty() || p.empty() || s.size() < p.size()){
return result;
}
vector<int> sCount(26, 0);
vector<int> pCount(26, 0);
for(char ch: p){
pCount[ch - 'a']++;
}
int k = p.size();
for(int i=0; i<k; i++){
sCount[s[i] - 'a']++;
}
if(sCount == pCount){
result.push_back(0);
}
int left = 0;
for(int right=k; right<s.size(); right++){
sCount[s[left] - 'a']--;
left++;
sCount[s[right] - 'a']++;
if(sCount == pCount){
result.push_back(left);
}
}
return result;
}
};class Solution:
def find_all_anagrams_in_string(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Find All Anagrams in a String
*/
function findAllAnagramsInString(...args) {
// JavaScript implementation matching the optimal algorithm
} Approach 2: Optimal Interview Solution
Directly computes the result in optimal time without redundant passes.
Approach 2: Optimal Interview Solution Implementation
Approach 2: Optimal Interview Solution
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList<>();
if(s == null || p == null || s.length() < p.length()){
return result;
}
int sCount[] = new int[26];
int pCount[] = new int[26];
int k = p.length();
for(int i=0; i<k; i++){
pCount[p.charAt(i) - 'a']++;
}
for(int i=0; i<k; i++){
sCount[s.charAt(i) - 'a']++;
}
if(Arrays.equals(sCount, pCount)){
result.add(0);
}
int left = 0;
for(int right=k; right<s.length(); right++){
sCount[s.charAt(left) - 'a']--;
left++;
sCount[s.charAt(right) - 'a']++;
if(Arrays.equals(sCount, pCount)){
result.add(left);
}
}
return result;
}
}class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> result;
if(s.empty() || p.empty() || s.size() < p.size()){
return result;
}
vector<int> sCount(26, 0);
vector<int> pCount(26, 0);
for(char ch: p){
pCount[ch - 'a']++;
}
int k = p.size();
for(int i=0; i<k; i++){
sCount[s[i] - 'a']++;
}
if(sCount == pCount){
result.push_back(0);
}
int left = 0;
for(int right=k; right<s.size(); right++){
sCount[s[left] - 'a']--;
left++;
sCount[s[right] - 'a']++;
if(sCount == pCount){
result.push_back(left);
}
}
return result;
}
};class Solution:
def find_all_anagrams_in_string(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Find All Anagrams in a String
*/
function findAllAnagramsInString(...args) {
// JavaScript implementation matching the optimal algorithm
} Edge Cases to Watch
- Empty or single-element input (n <= 1)
- Duplicates and boundary values near extrema
- Already sorted, reverse sorted, or uniform values
- Maximum constraint inputs to ensure no integer overflow or TLE
Master the Sliding Window Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Sliding Window pattern.
How to Recognize This Pattern
- Find all anagram positions
Complexity Analysis
Time Complexity
O(n)
Space Complexity
O(1)