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 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
🎯 Scrabble Tiles Problem VisualizationAvailable Tiles (chars = "atach")ATACHWord Checking Process"cat" → needs C(1), A(1), T(1)✓ Available! Length: 3"bt" → needs B(1), T(1)✗ No 'B' tile available!"hat" → needs H(1), A(1), T(1)✓ Available! Length: 3"tree" → needs T(1), R(1), E(2)✗ No 'R' or 'E' tiles!Result: 3 + 3 = 6Frequency CountA: 2 tilesC: 1 tile, H: 1 tile, T: 1 tile💡 Key Strategy1. Count available tiles once2. For each word, check if we have enough tiles3. Sum lengths of valid words
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

n
2n
Linear Growth
Space Complexity
O(1)

Hash maps use at most 26 entries for lowercase English letters (constant space)

n
2n
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
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
89.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen