Skip to main content

Find Words That Can Be Formed by Characters - Solution & Explanation

EasyArrayHash TableStringCounting15 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

You are given an array of strings words and a string chars.

A string is good if it can be formed by characters from chars (each character can only be used once).

Return the sum of lengths of all good strings in words.

 

Example 1:

Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

Example 2:

Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.

 

Constraints:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length, chars.length <= 100
  • words[i] and chars consist of lowercase English letters.

Approach Overview

Problem Overview: You receive a list of words and a string chars. Each character in chars can be used only once. The task is to determine which words can be constructed using those characters and return the total length of all valid words.

Approach 1: Character Frequency Counting (O(W * K) time, O(1) space)

The most efficient method relies on fixed-size frequency counting. First compute a frequency array of size 26 for chars. Then iterate through each word and build another 26-length counter for that word. Compare the counts: if any character appears more times in the word than in chars, the word cannot be formed. If all counts fit within the available supply, add the word’s length to the result. Since the alphabet is limited to lowercase English letters, the counter size remains constant, giving O(1) auxiliary space. This approach is straightforward and performs well because comparisons are constant-time operations over a small array.

This technique commonly appears in problems involving counting and string manipulation. You iterate through characters once, update counters, and validate availability using direct index access.

Approach 2: Map-based Character Counting (O(W * K) time, O(K) space)

This variation uses a hash map to track frequencies instead of a fixed array. Build a frequency map for chars. For each word, construct another map while iterating through its characters. During the check phase, verify that each character’s frequency in the word does not exceed the frequency in the base map. If all checks pass, accumulate the word length.

The logic mirrors the array-counting approach but uses dynamic storage rather than fixed indexing. Time complexity remains O(W * K), where W is the number of words and K is the average word length. Space complexity becomes O(K) due to per-word maps. This approach is useful when working with larger character sets or when the problem is framed around hash table operations.

Recommended for interviews: Character Frequency Counting. Interviewers expect you to recognize that the alphabet size is fixed and replace hash maps with a small array. The map-based approach still demonstrates correct reasoning, but the array solution shows stronger awareness of constant-space optimizations and typical patterns used in array-based counting problems.

Approach 1: Character Frequency Counting

This approach involves counting the frequency of each character in the 'chars' string. For each word, check if the word can be constructed using these characters, respecting their frequencies.

This solution counts the frequency of each character in the 'chars' array using an integer array of size 26. For each word, it creates a frequency array to check if the word can be formed without exceeding the available characters and their counts.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), as the auxiliary space used is constant, i.e., the two arrays of size 26.

Try this approach in the editor →

Approach 2: Map-based Character Counting

This approach utilizes hash maps to count the frequency of characters in both the 'chars' string and each word. The map allows for dynamic sizing and flexibility with character counts.

This solution counts the frequencies of characters using hash maps for both 'chars' and each word. It checks if the words can be formed by comparing counts in these maps and adds the lengths of words that can be formed.

Code

C++

Python

Complexity

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), additional space overhead is minimal with the unordered maps.

Try this approach in the editor →

Approach 3: Counting

We can use an array cnt of length 26 to count the occurrence of each letter in the string chars.

Then we traverse the string array words. For each string w, we use an array wc of length 26 to count the occurrence of each letter in the string w. If for each letter c, wc[c] leq cnt[c], then we can spell the string w with the letters in chars, otherwise we cannot spell the string w. If we can spell the string w, then we add the length of the string w to the answer.

After the traversal, we can get the answer.

The time complexity is O(L), and the space complexity is O(C). Here, L is the sum of the lengths of all strings in the problem, and C is the size of the character set. In this problem, C = 26.

Code

Python

Java

C++

Go

TypeScript

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Character Frequency Counting

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), as the auxiliary space used is constant, i.e., the two arrays of size 26.

Map-based Character Counting

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), additional space overhead is minimal with the unordered maps.

Counting

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Character Frequency Counting (Array)O(W * K)O(1)Best choice when characters are limited (e.g., lowercase English letters). Fast lookups and minimal memory.
Map-based Character CountingO(W * K)O(K)Useful when the character set is large or unknown and fixed-size arrays are not practical.

Video Solution

Find Words That Can Be Formed by Characters | META | Leetcode 1160codestorywithMIK13,876 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Words That Can Be Formed by Characters easy or hard?
Find Words That Can Be Formed by Characters is classified as an Easy problem on LeetCode with a high acceptance rate around 70%. The challenge mainly tests understanding of character counting, arrays, and hash maps rather than complex algorithms.
Find Words That Can Be Formed by Characters Python/Java solution
Python and Java implementations typically create an array or dictionary to store character frequencies of the given string. For each word, compute its frequency and verify it does not exceed the base counts. If valid, add the word length to the result. The algorithm remains O(W * K) in both languages.
How to solve Find Words That Can Be Formed by Characters in O(n)?
Build a frequency counter for the characters string, then iterate through each word and count its characters. For every character in the word, check if its frequency exceeds the available count. If all checks pass, add the word length to the answer. Since counting and comparisons are linear in word length, the overall complexity stays O(W * K).
What is the best approach for Find Words That Can Be Formed by Characters?
The most efficient approach uses character frequency counting with a fixed array of size 26. Count the frequency of characters in the input string and compare it with the frequency of each word. If every character in the word appears within the available count, add its length to the total. This approach runs in O(W * K) time and O(1) space.
Is Find Words That Can Be Formed by Characters asked at Google/Amazon/Meta?
Problems involving character frequency counting and hash maps frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the underlying pattern—verifying resource availability using frequency arrays—is a common interview concept.
What data structure is used in Find Words That Can Be Formed by Characters?
The typical solution uses a frequency array of size 26 to store counts of lowercase letters. Some implementations use a hash map instead, especially when the character set is not restricted. Both approaches rely on counting and quick lookup of character frequencies.
What is the time complexity of Find Words That Can Be Formed by Characters?
The time complexity is O(W * K), where W is the number of words and K is the average length of each word. Each word is scanned once to compute its character counts and compared against the base frequency array built from the characters string.

Ready to solve this problem?

Practice Find Words That Can Be Formed by Characters with our built-in code editor and test cases.

Practice on FleetCode