Strings

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Count characters in one string and subtract counts using the other string.

Step 1 / 5
Problem shape
Current lesson

Valid Anagram Lab

Strings Beginner

Valid Anagram

Welcome to the Valid Anagram algorithm lab. Count characters in one string and subtract counts using the other string. Use it when: Use when two strings must contain the same characters with the same frequencies. The time complexity is O(n), and the space complexity is O(k). The pseudocode is: if lengths differ return false count chars in first string for char in second string decrement count[char] return all counts are zero As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(k)

Pseudocode

if lengths differ return false
count chars in first string
for char in second string
  decrement count[char]
return all counts are zero
Open deeper structure lesson
1.0x