Total Length of Words Buildable from a Limited Pool of Letters
Company: TabaPay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Given a list of words and a pool of letters, compute the total length of the words that can be spelled when each pool letter is used at most once per word. It tests character frequency counting, independent per-word checks, and correct handling of repeated letters and repeated words in the input.
Constraints
- 1 <= len(words) <= 1000
- 1 <= len(words[i]) <= 100
- 1 <= len(chars) <= 100
- Every string contains only lowercase English letters a to z.
- The answer is at most 100,000.
Examples
Input: (['dog', 'god', 'good', 'do'], 'odgx')
Expected Output: 8
Explanation: Source example 1: 'dog', 'god' and 'do' are buildable (3 + 3 + 2); 'good' needs two 'o'.
Input: (['hello', 'world', 'low', 'owl'], 'lowrd')
Expected Output: 11
Explanation: Source example 2: 'hello' needs 'h' and 'e'; 'world', 'low' and 'owl' give 5 + 3 + 3.
Hints
- Whether a word can be built depends only on how many times each letter appears in the word compared with how many times it appears in chars.
- The pool is never used up: every word in the list, including each repeated copy, is compared against all of chars.
- A word can fail because it needs a letter that chars does not contain at all, or because it needs more copies of a letter than chars has.