Common String Patterns & Problem-Solving Techniques
Character Frequency Counting
Read aloud is unavailable in this browser.
The Most Fundamental String Pattern
If there is one pattern that appears more frequently in string problems than any other, it is character frequency counting. This deceptively simple technique—counting how many times each character appears in a string—forms the foundation for dozens of classic problems and real-world applications.
Before you can determine if two strings are anagrams, before you can find the first non-repeating character, before you can check if a string has all unique characters, before you can validate a ransom note against a magazine—you must first understand and internalize character frequency counting.
This page will not teach you a specific algorithm. Instead, it will teach you to think in frequencies—a mental model that will pay dividends throughout your DSA journey and professional career.
By the end of this page, you will understand how to conceptualize characters as countable entities, why frequency counting is such a powerful abstraction, the mental model behind building frequency representations, and how this pattern connects to a vast landscape of string problems.
What Is Character Frequency Counting?
At its core, character frequency counting answers a simple question: How many times does each character appear in a given string?
Consider the string "mississippi":
'm'appears 1 time'i'appears 4 times's'appears 4 times'p'appears 2 times
This mapping from characters to their counts is called a frequency distribution or frequency map. It transforms a sequential string into a statistical summary—collapsing positional information into aggregate counts.
When we compute character frequencies, we deliberately discard positional information. We no longer know that the 'm' is at position 0 or that the second 'i' is at position 4. What we retain is the compositional essence of the string—what it's made of, regardless of arrangement. This trade-off is precisely what makes frequency counting useful: many problems care only about composition, not order.
Formal Definition:
Given a string S of length n over an alphabet Σ (the set of all possible characters), the character frequency function f: Σ → ℕ maps each character c to the number of times it appears in S.
For S = "mississippi":
f('m') = 1
f('i') = 4
f('s') = 4
f('p') = 2
f('a') = 0 // Characters not in S have frequency 0
The sum of all frequencies equals the string length: Σ f(c) = n for all c ∈ Σ.
| Character | Frequency | Percentage |
|---|---|---|
| m | 1 | 9.1% |
| i | 4 | 36.4% |
| s | 4 | 36.4% |
| p | 2 | 18.2% |
| Total | 11 | 100% |
Why Frequency Counting Matters
Frequency counting is not just a technique—it's a lens for viewing strings. Understanding why this lens is so powerful will help you recognize when to apply it.
1. It Reduces Complexity
A string of length n has n positions to reason about. Its frequency representation has at most |Σ| entries (the size of the alphabet). For ASCII strings, that's at most 128 entries; for lowercase English letters, just 26. This dramatic reduction in dimensionality makes many comparisons tractable.
2. It Enables O(n) Solutions to Problems That Seem Harder
Naive approaches to anagram detection might sort both strings (O(n log n)) and compare. Frequency counting achieves the same in O(n) time. This pattern repeats: frequency counting often unlocks linear-time solutions.
Whenever you see a problem that asks about character composition, distribution, matching, or balance—frequency counting should immediately come to mind. The skill isn't in implementing it (that's trivial); the skill is in recognizing when it applies.
3. It Decouples 'What' from 'Where'
Many string problems don't care about position—they care about content. Does this string contain the right letters? Are these two strings made of the same characters? Does this string have duplicates? Frequency counting extracts exactly the information needed to answer these questions.
4. It Composes with Other Techniques
Frequency counting combines elegantly with:
- Sliding windows: Maintain frequencies of characters in a moving window
- Two pointers: Track frequencies between pointer positions
- Hashing: Use frequency signatures for fast comparisons
- Dynamic programming: Frequencies define subproblem states
- •Anagram Detection: Are two strings anagrams of each other?
- •Valid Anagram Groups: Group all anagrams together from a list of strings
- •First Unique Character: Find the first character that appears exactly once
- •Check Permutation Inclusion: Does string A contain a permutation of string B as a substring?
- •Ransom Note Validation: Can you construct string A using characters from string B?
- •Minimum Window Substring: Find the smallest window containing all characters of a pattern
- •Check for Duplicates: Does the string contain any duplicate characters?
- •Character Replacement: Longest substring with at most k replacements to make all characters same
Building the Mental Model
To truly internalize frequency counting, you need to develop a strong mental model. Let's build this model step by step.
Step 1: See Strings as Bags, Not Sequences
A string like "abc" can be viewed two ways:
- Sequence view: A list of characters in order —
['a', 'b', 'c'] - Bag view: A multiset of characters ignoring order —
{a: 1, b: 1, c: 1}
The bag view (also called a multiset) is exactly what frequency counting produces. When you train yourself to see strings as bags when appropriate, many problems become simpler.
Characters have positions. Order matters. "abc" ≠ "bac". Used when position is significant (e.g., substring matching, palindrome checking).
Characters have counts. Order ignored. "abc" = "bac" (same bag). Used when only composition matters (e.g., anagram detection, character coverage).
Step 2: Visualize the Frequency Array/Map
Imagine a row of buckets, one for each possible character. As you scan the string, you drop each character into its corresponding bucket. At the end, the bucket heights represent the frequency distribution.
For lowercase English letters (26 characters), this is literally an array of 26 integers. For general Unicode, it's a hash map.
String: "banana"
Buckets after processing:
a b c ... n ... z
[ 3 ] [1] [ ] ... [2] ... [ ]
Step 3: Understand the Invariants
A frequency map maintains these invariants:
- Non-negative counts: Every frequency ≥ 0
- Sum equals length: Total of all frequencies = string length
- Idempotent representation: Same string always produces same frequencies
Think of the frequency representation as a 'canonical form' or 'fingerprint' of the string's composition. Two strings with identical frequency fingerprints can be rearranged into each other (they're anagrams). This fingerprint comparison is O(|Σ|), which is effectively O(1) for fixed alphabets like lowercase English.
Representation Strategies
There are multiple ways to represent character frequencies, each with different trade-offs. Understanding these will help you choose appropriately based on problem constraints.
Strategy 1: Fixed-Size Array (When Alphabet is Known and Small)
For problems involving only lowercase letters (a-z), use an array of 26 integers. Index 0 corresponds to 'a', index 1 to 'b', and so on.
Advantages:
- Cache-friendly, contiguous memory
- Direct indexing: O(1) access via
char - 'a' - No hashing overhead
- Easy comparison: just compare arrays
Disadvantages:
- Wastes space if only few characters are present
- Not suitable for large alphabets (Unicode)
When to use: Interview problems with explicit constraints like "string contains only lowercase letters."
12345678910111213141516
// Conceptual pseudocode for array-based frequency counting// For string over lowercase English alphabet FUNCTION buildFrequencyArray(str): freq = array of 26 zeros // One slot per letter a-z FOR each character c in str: index = (c - 'a') // 'a'→0, 'b'→1, ..., 'z'→25 freq[index] += 1 // Increment count RETURN freq // Usage example:// buildFrequencyArray("banana")// Returns: [3, 1, 0, 0, ..., 2, ..., 0]// a b c d n zStrategy 2: Hash Map (When Alphabet is Unknown or Large)
For Unicode strings, mixed-case text, or when the character set isn't specified, use a hash map (dictionary) that maps characters to counts.
Advantages:
- Only stores characters that actually appear (sparse representation)
- Works with any character set, including Unicode
- Flexible for dynamic alphabets
Disadvantages:
- Hashing overhead per access
- Less cache-friendly than arrays
- Comparison requires iterating over entries
When to use: Real-world applications, problems without explicit alphabet constraints, or when memory efficiency matters for sparse data.
1234567891011121314151617
// Conceptual pseudocode for hash map-based frequency counting// Works with any alphabet FUNCTION buildFrequencyMap(str): freq = empty hash map FOR each character c in str: IF c exists in freq: freq[c] += 1 ELSE: freq[c] = 1 RETURN freq // Usage example:// buildFrequencyMap("Hello World!")// Returns: {'H':1, 'e':1, 'l':3, 'o':2, ' ':1, 'W':1, 'r':1, 'd':1, '!':1}| Aspect | Fixed Array | Hash Map |
|---|---|---|
| Space Complexity | O(|Σ|) always | O(unique chars) |
| Time per Access | O(1) direct index | O(1) amortized hash |
| Comparison | O(|Σ|) array compare | O(unique chars) iteration |
| Best For | Small, known alphabet | Large/unknown alphabet |
| Memory Locality | Excellent (contiguous) | Poor (hash buckets) |
Common Frequency Counting Patterns
Once you have a frequency representation, several patterns emerge for using it. These patterns form the building blocks of frequency-based solutions.
Pattern 1: Build and Compare
The most direct pattern: build frequency representations for two strings and compare them. If the frequencies match, the strings share the same character composition.
Use case: Anagram detection, permutation checking.
IsAnagram(s1, s2):
IF length(s1) ≠ length(s2): RETURN false
RETURN buildFrequency(s1) == buildFrequency(s2)
Pattern 2: Build and Query
Build a frequency map once, then answer multiple queries against it. This amortizes the O(n) build cost over many O(1) queries.
Use case: Finding first unique character (query for freq == 1), checking if character exists.
FirstUniqueChar(s):
freq = buildFrequency(s)
FOR i = 0 to length(s) - 1:
IF freq[s[i]] == 1:
RETURN i
RETURN -1 // No unique character
Pattern 3: Incremental Frequency (Additive)
Instead of building frequencies for a fixed string, incrementally add characters as you process them. This is crucial for sliding window and streaming scenarios.
ProcessStream(stream):
freq = empty frequency map
FOR each character c in stream:
freq[c] += 1
// Answer queries based on current freq state
Pattern 4: Incremental Frequency (Additive and Subtractive)
The most powerful pattern: add characters when they enter a region, subtract when they leave. This enables efficient sliding window algorithms.
SlidingWindowFrequency(s, windowSize):
freq = empty frequency map
// Build initial window
FOR i = 0 to windowSize - 1:
freq[s[i]] += 1
// Slide the window
FOR i = windowSize to length(s) - 1:
freq[s[i]] += 1 // Add new char
freq[s[i - windowSize]] -= 1 // Remove old char
// Process current window state
When subtracting frequencies, counts can become zero. Depending on your comparison logic, you may need to remove zero-count entries from hash maps to ensure accurate comparisons. A character with count 0 and a missing character should be equivalent for most problems.
The Frequency Counting Mindset
Developing the frequency counting mindset means training yourself to ask certain questions when you encounter a string problem:
Question 1: Does order matter for this problem?
If the answer is "no," frequency counting is almost certainly relevant. Problems about composition, coverage, or rearrangement typically don't care about order.
Question 2: Am I comparing or matching?
Anytime you're checking if two strings are "equivalent" in some compositional sense—same characters, same character counts, one is a subset of another—frequency counting provides the comparison mechanism.
Question 3: Is there a "budget" of characters?
Problems that ask "can you form X using characters from Y?" are budget problems. Y provides a budget (its frequencies), and X's frequencies must fit within that budget.
Question 4: Am I tracking a sliding subset?
If you need to track character composition within a moving window, incremental frequency updates are the right tool.
- •"Are these strings anagrams?" → Frequency comparison
- •"Find all anagrams of X in Y" → Sliding window + frequency
- •"Can you spell A using letters from B?" → Frequency budget check
- •"Find the first unique character" → Frequency == 1 query
- •"Check if string has all unique characters" → Frequency <= 1 check
- •"Minimum window containing all characters of pattern" → Sliding window + frequency coverage
- •"Longest substring with at most k distinct characters" → Frequency count + sliding window
- •"Valid parentheses balance" → Special case: frequency of '(' minus ')' tracking
Expert problem solvers don't think "I should use frequency counting." They immediately see the problem as a frequency problem. The technique becomes invisible—it's simply how they perceive the problem structure. Developing this automatic recognition comes from solving many problems and reflecting on what made each solution work.
Complexity Analysis
Understanding the complexity of frequency operations is essential for analyzing overall algorithm performance.
Building a Frequency Representation
For a string of length n:
- Time Complexity: O(n) — We visit each character exactly once
- Space Complexity: O(|Σ|) or O(min(n, |Σ|))
- Array representation: O(|Σ|) where |Σ| is alphabet size
- Hash map representation: O(min(n, |Σ|)) — at most n unique characters, bounded by |Σ|
Comparing Two Frequency Representations
For frequency maps of strings of length n and m:
- Time Complexity: O(|Σ|) or O(min(n, m, |Σ|))
- Array comparison: O(|Σ|) — compare all slots
- Hash map comparison: O(unique chars) — iterate over entries
- For fixed small alphabets (e.g., 26 letters): effectively O(1)
| Operation | Time | Space | Notes |
|---|---|---|---|
| Build frequency | O(n) | O(|Σ|) | n = string length, |Σ| = alphabet size |
| Query single char | O(1) | O(1) | Direct lookup |
| Compare frequencies | O(|Σ|) | O(1) | Comparing two maps |
| Add char to freq | O(1) | O(1) | Increment count |
| Remove char from freq | O(1) | O(1) | Decrement count |
| Check if empty | O(|Σ|) or O(1)* | O(1) | *O(1) if tracking non-zero count |
For problems over fixed alphabets (like lowercase English letters), |Σ| is a constant (26). This means O(|Σ|) operations are effectively O(1). This is why frequency-based anagram checking is considered O(n) rather than O(n + 26)—the constant factor is absorbed.
Why O(n) Matters
Frequency counting provides O(n) solutions to problems where naive approaches might be O(n²) or O(n log n):
| Problem | Naive Approach | Frequency Approach |
|---|---|---|
| Anagram check | Sort both: O(n log n) | Frequency compare: O(n) |
| Find duplicates | Nested loop: O(n²) | Frequency > 1: O(n) |
| First unique | Check each against all: O(n²) | Frequency == 1: O(n) |
This improvement from O(n²) to O(n) is often the difference between a solution that times out and one that passes.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.