Find Words That Can Be Formed by Characters

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array of strings words and a string chars. A word is **good** if it can be spelled using letters from chars, where each character of chars can be used at most once per word (every word starts again from the full chars). Return the sum of the lengths of all good words. Words that appear several times in words are counted each time.

Examples

Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
`"cat"` and `"hat"` can be spelled, giving 3 + 3.
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
`"hello"` and `"world"` fit; `"leetcode"` needs letters that are missing.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems