BackmediumSliding WindowSalesforce

Anagram Index Finder Solution

Problem Statement

Given a string sequence and a string pattern, find all starting positions in sequence where pattern or its anagram occurs. Return a list of such positions.

Example 1
Input
sequence = 'abxaba', pattern = 'ab'
Output
[0, 3, 4]

Explanation: Step-by-step: with input sequence 'abxaba' and pattern 'ab', we find all starting positions where pattern or its anagram occurs. We start by sorting the pattern 'ab' to get 'ab'. Then we iterate over the sequence with a sliding window of size 2 (length of pattern). At each position, we sort the substring in the window and compare it with the sorted pattern. If they match, we add the current position to the result list. So, the output is [0, 3, 4].

Example 2
Input
sequence = 'abcabc', pattern = 'bca'
Output
[1, 4]

Explanation: Step-by-step: with input sequence 'abcabc' and pattern 'bca', we find all starting positions where pattern or its anagram occurs. We start by sorting the pattern 'bca' to get 'abc'. Then we iterate over the sequence with a sliding window of size 3 (length of pattern). At each position, we sort the substring in the window and compare it with the sorted pattern. If they match, we add the current position to the result list. So, the output is [1, 4].

Constraints

  • 1 <= s.length, p.length <= 3 * 10^4
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Anagram Index Finder — Problem Statement & Solution Guide

Sliding WindowMediumSliding Window / Hash Map
TimeO(n*m)
|
SpaceO(m)

Problem Description

Given a string sequence and a string pattern, find all starting positions in sequence where pattern or its anagram occurs. Return a list of such positions.

Examples

Example 1

Input

sequence = 'abxaba', pattern = 'ab'

Output

[0, 3, 4]

Explanation: Step-by-step: with input sequence 'abxaba' and pattern 'ab', we find all starting positions where pattern or its anagram occurs. We start by sorting the pattern 'ab' to get 'ab'. Then we iterate over the sequence with a sliding window of size 2 (length of pattern). At each position, we sort the substring in the window and compare it with the sorted pattern. If they match, we add the current position to the result list. So, the output is [0, 3, 4].

Example 2

Input

sequence = 'abcabc', pattern = 'bca'

Output

[1, 4]

Explanation: Step-by-step: with input sequence 'abcabc' and pattern 'bca', we find all starting positions where pattern or its anagram occurs. We start by sorting the pattern 'bca' to get 'abc'. Then we iterate over the sequence with a sliding window of size 3 (length of pattern). At each position, we sort the substring in the window and compare it with the sorted pattern. If they match, we add the current position to the result list. So, the output is [1, 4].

Constraints

  • 1 <= s.length, p.length <= 3 * 10^4

Optimal Approach & Strategy

Sliding window with character frequency arrays. If arrays match, increment count. Slide by decrementing outgoing char and incrementing incoming char. Time O(N), Space O(1).

Brute Force Approach

Compare every p.length substring of s with p. Time O(N*M).

Verified Code Solutions

JavaScript Solution
Time: O(n*m)
function findAnagramPositions(sequence, pattern) { 
       let result = []; 
       let patternLength = pattern.length; 
       let sortedPattern = pattern.split('').sort().join(''); 
       for (let i = 0; i <= sequence.length - patternLength; i++) { 
           let substring = sequence.substring(i, i + patternLength); 
           let sortedSubstring = substring.split('').sort().join(''); 
           if (sortedSubstring === sortedPattern) { 
               result.push(i); 
           } 
       } 
       return result; 
   }

Asked in Top Tech Interviews

Salesforce

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.