Advertisement

Count the Number of Consistent Strings - LeetCode 1684 Solution

Count the Number of Consistent Strings - Complete Solution Guide

Count the Number of Consistent Strings is LeetCode problem 1684, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a string allowed consisting of distinct characters and an array of strings words . A string is consistent if all characters in the string appear in the string allowed . Return the number of consistent strings in the array words . Example 1: Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] Output: 2 Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'. Example 2: Input: allowed = "abc", words = ["a","b","c","ab","ac","bc"

Detailed Explanation

The problem asks you to count the number of strings in a given array `words` that are considered "consistent." A string is consistent if all its characters are present in another string, `allowed`. The `allowed` string contains only unique characters. The input consists of the `allowed` string and an array of strings `words`. The output is a single integer representing the count of consistent strings.

Solution Approach

The provided solutions all follow a similar approach: they iterate through each string in the `words` array. For each string, they iterate through its characters, checking if each character is present in the `allowed` string. If a character is not found in `allowed`, the string is marked as inconsistent, and the inner loop breaks. If the inner loop completes without finding any disallowed characters, the string is counted as consistent. Different implementations use different data structures (sets, boolean arrays) to optimize the character lookup process.

Step-by-Step Algorithm

  1. Create a data structure (set or boolean array) to efficiently store and check the characters in `allowed`.
  2. Iterate through each string in the `words` array.
  3. For each string, iterate through its characters.
  4. For each character, check if it's present in the `allowed` data structure.
  5. If a character is not found in `allowed`, mark the string as inconsistent and break the inner loop.
  6. If the inner loop completes without finding any inconsistent characters, increment the count of consistent strings.
  7. Return the final count of consistent strings.

Key Insights

  • Using a set for `allowed` allows for O(1) lookup time to check if a character is allowed.
  • Iterating through each word and checking each character against the `allowed` set is a straightforward approach.
  • The problem can be efficiently solved using a boolean array to represent allowed characters instead of a set for potential memory optimizations, especially for larger alphabets.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, String, Bit Manipulation, Counting.

Companies

Asked at: Robinhood.