Total Length of Independently Formable Words

Quick Overview

Total Length of Independently Formable Words. For each word in words, determine whether its letters can be taken from chars, using each occurrence in chars at most once for that word.

Total Length of Independently Formable Words

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem For each word in words, determine whether its letters can be taken from chars, using each occurrence in chars at most once for that word. Reset the available letters for the next word. Return the sum of lengths of all formable input words. ```cpp class Solution { public: int countCharacters(vector<string>& words, string chars); }; ``` ### Examples **Example 1:** ```text Input: words = ["ab","aa","ba"], chars = "aab" Output: 6 Explanation: Each word separately fits the available two a letters and one b, contributing2 each. ``` **Example 2:** ```text Input: words = ["zz","z","zzz"], chars = "zz" Output: 3 Explanation: zz and z fit; zzz requires an unavailable third z. ``` ### Constraints - There are 1 to 1000 words. - Each word and chars has length 1 to 100. - All characters are lowercase English letters. ### Contract considerations Characters may be rearranged to form a word; their positions in chars impose no order. ```hint Read the boundary condition Are letters consumed across different words, or is the supply restored for each input entry? ``` ### Boundary reminders Characters may be rearranged to form a word; their positions in chars impose no order.

Overview: Total Length of Independently Formable Words. For each word in words, determine whether its letters can be taken from chars, using each occurrence in chars at most once for that word.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

For each word in words, determine whether its letters can be taken from chars, using each occurrence in chars at most once for that word. Reset the available letters for the next word. Return the sum of lengths of all formable input words.

class Solution {
public:
    int countCharacters(vector<string>& words, string chars);
};

Examples

Example 1:

Input: words = ["ab","aa","ba"], chars = "aab"
Output: 6
Explanation: Each word separately fits the available two a letters and one b, contributing2 each.

Example 2:

Input: words = ["zz","z","zzz"], chars = "zz"
Output: 3
Explanation: zz and z fit; zzz requires an unavailable third z.

Constraints

  • There are 1 to 1000 words.
  • Each word and chars has length 1 to 100.
  • All characters are lowercase English letters.

Contract considerations

Characters may be rearranged to form a word; their positions in chars impose no order.

Boundary reminders

Characters may be rearranged to form a word; their positions in chars impose no order.