Find Words Formed by Characters

Easy
3
44.4% Acceptance

In this lab, you will be working on a function called countCharacters that takes in two inputs - an array of strings words and a string chars. The goal of this function is to find good strings from the words array that can be formed by characters from the chars string, with each character being used only once. The function should return the sum of lengths of all good strings found in the words array.

To make it clearer, let's go through a couple of examples:

Example 1:

const words = ["cat", "bt", "hat", "tree"]; const chars = "atach"; const result = countCharacters(words, chars); // The output should be 6

Explanation: The strings "cat" and "hat" can be formed using characters from the chars string, so the result is the sum of their lengths: 3 + 3 = 6.

Example 2:

const words = ["hello", "world", "leetcode"]; const chars = "welldonehoneyr"; const result = countCharacters(words, chars); // The output should be 10

Explanation: The strings "hello" and "world" can be formed using characters from the chars string, so the result is the sum of their lengths: 5 + 5 = 10.

Keep in mind the following constraints:

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

Throughout this lab, you will face various challenges. Make sure to complete each challenge as specified in the instructions and export every variable, function, or object required for the evaluation.

Remember that the evaluation script is very critical. Ensure to write it without leaving anything out and with no errors. The testlog array's final length should match the number of challenges, and the order of the evaluation script's try-catch blocks should correspond to the written challenges.