Find Words Formed by Characters

Easy

Description

Given an array of strings words and a string chars, return the sum of lengths of all good words. A word is good if all characters can be formed using chars.

Examples

Input:words = ["cat","bt","hat","tree"], chars = "atach"
Output:6
Explanation:

cat and hat = 6.

Input:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output:10
Explanation:

"hello" can be formed (h-1, e-1, l-2, o-1 all available) = 5 chars. "world" can be formed (w-1, o-1, r-1, l-1, d-1 all available) = 5 chars. "leetcode" cannot be formed (needs 3 e's but only 2 available). Total: 5 + 5 = 10.

Input:words = ["a","aa","aaa"], chars = "aa"
Output:3
Explanation:

"a" can be formed using 1 'a' = 1 char. "aa" can be formed using both 'a's = 2 chars. "aaa" cannot be formed (needs 3 'a's but only 2 available). Total: 1 + 2 = 3.

Constraints

  • 1 ≤ words.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!