Find Words That Can Be Formed by Characters - Problem
Find Words That Can Be Formed by Characters
Imagine you have a collection of letter tiles (like in Scrabble) represented by a string
Your task is to determine which words are "good" - meaning they can be completely formed using the available characters. Return the sum of lengths of all good words.
Example: If
• "cat" ✅ (uses: c-1, a-1, t-1)
• "bt" ❌ (no 'b' available)
• "hat" ✅ (uses: h-1, a-1, t-1)
• "tree" ❌ (needs 2 'e's, but none available)
Good words: "cat" (3) + "hat" (3) = 6
Imagine you have a collection of letter tiles (like in Scrabble) represented by a string
chars, and you want to spell out words from an array words. Each letter tile can only be used once per word, but you can reuse the same tiles for different words.Your task is to determine which words are "good" - meaning they can be completely formed using the available characters. Return the sum of lengths of all good words.
Example: If
chars = "atach" and words = ["cat", "bt", "hat", "tree"], then:• "cat" ✅ (uses: c-1, a-1, t-1)
• "bt" ❌ (no 'b' available)
• "hat" ✅ (uses: h-1, a-1, t-1)
• "tree" ❌ (needs 2 'e's, but none available)
Good words: "cat" (3) + "hat" (3) = 6
Input & Output
example_1.py — Basic Case
$
Input:
words = ["cat","bt","hat","tree"], chars = "atach"
›
Output:
6
💡 Note:
"cat" can be formed (c-1, a-1, t-1), "hat" can be formed (h-1, a-1, t-1). "bt" cannot be formed (no 'b'), "tree" cannot be formed (needs 2 'e's but none available). Total: len("cat") + len("hat") = 3 + 3 = 6
example_2.py — All Words Valid
$
Input:
words = ["hello","world","leetcode"], chars = "welldonehoneyr"
›
Output:
10
💡 Note:
"hello" can be formed, "world" can be formed. "leetcode" cannot be formed (needs 3 'e's but only 2 available, needs 2 'o's but only 1 available). Total: 5 + 5 = 10
example_3.py — No Valid Words
$
Input:
words = ["dyiclysmffuhibgfvapygkorkqllqlvokosagyelotobicwcmebnpznjbirzrzsrtzjxhsfpiwyfhzyonmuabtlwin"], chars = "usdruypficfbpfbivlrhutcgvyjenlxzeovdyjtgvvfdjzcmikjraspdfp"
›
Output:
0
💡 Note:
The single very long word cannot be formed with the available characters (missing several required characters). Total: 0
Visualization
Tap to expand
Understanding the Visualization
1
Organize Your Tiles
Count each letter tile you have available
2
Check Each Word
For each word, see if you have enough tiles to spell it
3
Add Valid Words
Sum up the lengths of words you can successfully spell
4
Return Total Length
The answer is the total length of all possible words
Key Takeaway
🎯 Key Insight: By counting character frequencies once and reusing this information for all word validations, we transform an O(n×m×k) brute force solution into an optimal O(n×m + k) solution.
Time & Space Complexity
Time Complexity
O(n*m + k)
O(k) to count chars once, then O(n*m) to process all words where n=number of words, m=average word length
✓ Linear Growth
Space Complexity
O(1)
Hash maps use at most 26 entries for lowercase English letters (constant space)
✓ Linear Space
Constraints
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length, chars.length ≤ 100
- words[i] and chars consist of lowercase English letters only
- Each character in chars can be used at most once per word
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code