101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Character Frequency Counting - Complex approach
  2. Sorting Approach - Complex approach

Step 1: Clean the Input

Before checking if two strings are anagrams, we need to normalize them by removing spaces and converting to lowercase.

  1. Convert all characters to lowercase
  2. Remove all non-alphanumeric characters (spaces, punctuation, etc.)
  3. This ensures we're comparing only the relevant characters

Example:

Original:
Astronomer vs Moon starer
Cleaned:
astronomer vs moonstarer

Approach 1: Character Frequency Counting

This approach uses a hash table (or an array for limited character sets) to count the frequency of each character in both strings. If the strings are anagrams, they will have the same characters with the same frequencies.

Algorithm:

  1. Clean both input strings (convert to lowercase, remove spaces).
  2. If the cleaned strings have different lengths, return false immediately.
  3. Create a hash table or array to count character frequencies.
  4. Iterate through the first string, incrementing the count for each character.
  5. Iterate through the second string, decrementing the count for each character.
  6. If any count becomes negative or remains positive after processing both strings, return false.
  7. Otherwise, return true.

Time Complexity:

O(n)

where n is the length of the strings. We need to iterate through each character once.

Space Complexity:

O(k)

where k is the size of the character set. For English alphabet, this is O(26), which simplifies to O(1).

Approach 2: Sorting Approach

Another approach is to sort both strings and then compare them. If they are anagrams, the sorted strings will be identical.

Algorithm:

  1. Clean both input strings (convert to lowercase, remove spaces).
  2. If the cleaned strings have different lengths, return false immediately.
  3. Sort the characters in both strings.
  4. Compare the sorted strings. If they are identical, return true; otherwise, return false.

Time Complexity:

O(n log n)

where n is the length of the strings. The sorting operation dominates the time complexity.

Space Complexity:

O(n)

We need to create sorted copies of the strings, which requires space proportional to their length.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function isAnagram(s, t):
// Clean the input strings
s = cleanString(s)
t = cleanString(t)
// Check if lengths are equal
if s.length != t.length:
return false
// Create a frequency counter
counter = new HashMap<Character, Integer>()
// Count characters in first string
for char in s:
counter[char] = counter.getOrDefault(char, 0) + 1
// Decrement counts for second string
for char in t:
counter[char] = counter.getOrDefault(char, 0) - 1
if counter[char] < 0:
return false
// Check if all counts are zero
for count in counter.values():
if count != 0:
return false
return true
ProblemSolutionCode
101 Logo
onenoughtone