The Find Words That Can Be Formed by Characters interview question is a resource-limited string matching task. You are given a list of words and a string of available chars. A word is "good" if it can be formed by characters from chars (each character can only be used as many times as it appears in chars). You need to return the sum of the lengths of all good words.
Companies like Apple and PayPal ask the Find Words That Can Be Formed coding problem to evaluate your proficiency with Hash Table interview patterns. It evaluations whether you can use frequency maps to efficiently compare the character requirements of a word against a fixed pool of resources.
This problem follows the Frequency Counting pattern.
chars string.words list:
wordCount[char] <= poolCount[char], the word is good.words = ["cat", "bt", "hat"], chars = "atach"
{a:2, t:1, c:1, h:1}{c:1, a:1, t:1}. (All counts chars map). Good! (Length 3).{b:1, t:1}. ('b' is not in chars). Not Good.{h:1, a:1, t:1}. (All counts chars map). Good! (Length 3).
Total: .chars map while checking a word, which ruins the check for subsequent words. Always use a copy or compare counts directly.For alphabet-related problems (a-z), an array of size 26 is almost always better than a Hash Map. It provides access with a very small constant factor and is a common optimization noted by experienced candidates in Hash Table interview patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Most Common Word | Easy | Solve | |
| Kth Distinct String in an Array | Easy | Solve | |
| Count Common Words With One Occurrence | Easy | Solve | |
| Subdomain Visit Count | Medium | Solve | |
| Find the Most Common Response | Medium | Solve |