Count Words Using Only Permitted Letters

Quick Overview

Count Words Using Only Permitted Letters. allowed lists distinct lowercase letters. Count the entries of words whose every character appears in allowed. Repeated letters inside a word are permitted.

Count Words Using Only Permitted Letters

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem `allowed` lists distinct lowercase letters. Count the entries of `words` whose every character appears in allowed. Repeated letters inside a word are permitted. ```cpp class Solution { public: int countConsistentStrings(string allowed, vector<string>& words); }; ``` ### Examples **Example 1:** ```text allowed = "xy", words = ["xx","yx","xyz","z"] Output: 2 Explanation: Only xx and yx use exclusively x and y. ``` **Example 2:** ```text allowed = "m", words = ["m","mm","mn","m"] Output: 3 Explanation: Count duplicate word entries separately. ``` ### Constraints - `1 <= words.length <= 10^4`. - `1 <= allowed.length <= 26`, with distinct letters. - `1 <= words[i].length <= 10`. - Every input character is a lowercase English letter. ### Clarifying Questions Allowed letters may be reused any number of times. Count entries rather than distinct word strings. ```hint Inspect the structure Does `allowed` describe letter membership or a limited supply of letter occurrences? ``` ### Reasoning checkpoints - Apply letter permission as reusable membership, not a consumable inventory. - Count qualifying word entries with their multiplicity, including repeated characters within a word. ### Optional extensions How would a 26-bit mask encode the same membership test?

Overview: Count Words Using Only Permitted Letters. allowed lists distinct lowercase letters. Count the entries of words whose every character appears in allowed. Repeated letters inside a word are permitted.

Google logo
Google
Jun 4, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

allowed lists distinct lowercase letters. Count the entries of words whose every character appears in allowed. Repeated letters inside a word are permitted.

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

Examples

Example 1:

allowed = "xy", words = ["xx","yx","xyz","z"]
Output: 2
Explanation: Only xx and yx use exclusively x and y.

Example 2:

allowed = "m", words = ["m","mm","mn","m"]
Output: 3
Explanation: Count duplicate word entries separately.

Constraints

  • 1 <= words.length <= 10^4 .
  • 1 <= allowed.length <= 26 , with distinct letters.
  • 1 <= words[i].length <= 10 .
  • Every input character is a lowercase English letter.

Clarifying Questions Guidance

Allowed letters may be reused any number of times. Count entries rather than distinct word strings.

Reasoning checkpoints

  • Apply letter permission as reusable membership, not a consumable inventory.
  • Count qualifying word entries with their multiplicity, including repeated characters within a word.

Optional extensions

How would a 26-bit mask encode the same membership test?