ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find All Anagrams in a String

MATLAB

Goal -- WPM

Ready
Exercise Algorithm Area
1function indices = findAnagrams(s, p)
2% Finds all start indices of p's anagrams in s.
3% Uses a sliding window approach.
4
5n = length(s);
6m = length(p);
7indices = [];
8
9% Edge case: if pattern is longer than string, no anagrams possible.
10if m > n
11return;
12end
13
14% Frequency maps for pattern and current window.
15pFreq = containers.Map('KeyType', 'char', 'ValueType', 'double');
16sFreq = containers.Map('KeyType', 'char', 'ValueType', 'double');
17
18% Initialize frequency map for pattern.
19for i = 1:m
20charP = p(i);
21if isKey(pFreq, charP)
22pFreq(charP) = pFreq(charP) + 1;
23else
24pFreq(charP) = 1;
25end
26end
27
28% Initialize frequency map for the first window in s.
29for i = 1:m
30charS = s(i);
31if isKey(sFreq, charS)
32sFreq(charS) = sFreq(charS) + 1;
33else
34sFreq(charS) = 1;
35end
36end
37
38% Check the first window.
39if isequal(pFreq, sFreq)
40indices = [indices, 1];
41end
42
43% Slide the window across s.
44for i = m + 1:n
45% Add the new character to the window.
46newChar = s(i);
47if isKey(sFreq, newChar)
48sFreq(newChar) = sFreq(newChar) + 1;
49else
50sFreq(newChar) = 1;
51end
52
53% Remove the character leaving the window.
54oldChar = s(i - m);
55sFreq(oldChar) = sFreq(oldChar) - 1;
56if sFreq(oldChar) == 0
57remove(sFreq, oldChar);
58end
59
60% Check if the current window is an anagram.
61if isequal(pFreq, sFreq)
62indices = [indices, i - m + 1];
63end
64end
65end
Algorithm description viewbox

Find All Anagrams in a String

Algorithm description:

This function finds all occurrences of anagrams of a pattern string `p` within a larger text string `s`. It employs a sliding window technique, maintaining frequency counts of characters within the window and comparing them to the pattern's frequency counts. This is useful in text processing, bioinformatics, and pattern matching scenarios.

Algorithm explanation:

The algorithm uses a sliding window of size equal to the length of the pattern `p`. Two frequency maps (hash maps or dictionaries) are maintained: one for the pattern `p` and one for the current window in `s`. The pattern's frequency map is computed once. The window's frequency map is initialized with the first `m` characters of `s`. Then, the window slides one character at a time. For each slide, the character entering the window is added to `sFreq`, and the character leaving the window is removed. If `sFreq` becomes equal to `pFreq`, it signifies an anagram, and the starting index of the window is recorded. The time complexity is O(N), where N is the length of `s`, because each character is processed a constant number of times. The space complexity is O(K), where K is the size of the character set (e.g., 26 for lowercase English letters), for storing the frequency maps. Edge cases include an empty pattern, an empty string, or a pattern longer than the string, all of which are handled by returning an empty result or early exit.

Pseudocode:

function findAnagrams(s, p):
  n = length(s)
  m = length(p)
  result_indices = []

  if m > n:
    return result_indices

  p_freq = map()
  s_freq = map()

  // Initialize p_freq
  for each char c in p:
    increment count of c in p_freq

  // Initialize s_freq for the first window
  for i from 0 to m-1:
    increment count of s[i] in s_freq

  if p_freq == s_freq:
    add 0 to result_indices

  // Slide the window
  for i from m to n-1:
    add s[i] to s_freq
    remove s[i-m] from s_freq (decrement count, remove if zero)

    if p_freq == s_freq:
      add i - m + 1 to result_indices

  return result_indices