Find Words That Can Be Formed by Characters

You are given an array of strings words and a string chars.

A string is good if it can be formed by characters from chars; each character can only be used once for each word in words.

Return the sum of lengths of all good strings in words.

Example 1
Inputwords = ["cat","bt","hat","tree"], chars = "atach"
Output6
The strings that can be formed are "cat" and "hat", so the answer is 3 + 3 = 6.
Example 2
Inputwords = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output10
The strings that can be formed are "hello" and "world", so the answer is 5 + 5 = 10.

Constraints

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

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate