Similar Problems

Similar Problems not available

Count The Number Of Consistent Strings - Leetcode Solution

Companies:

LeetCode:  Count The Number Of Consistent Strings Leetcode Solution

Difficulty: Easy

Topics: string hash-table bit-manipulation array  

Problem Statement:

You are given a string allowed consisting of distinct characters and a string[] 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.

Constraints:

1 <= words.length <= 104. 1 <= allowed.length <= 26. 1 <= words[i].length <= 10. The characters in allowed are distinct. words[i] and allowed contain only lowercase English letters.

Approach:

In this problem, we need to check whether all the characters in the given string are present in the allowed string or not. We will iterate over each string in the given words array and check if all the characters in that string are present in the allowed string. If all the characters are present, we will increment the count.

Steps:

  1. Initialize a count variable to zero.

  2. Iterate over each string in the given words array. a. Initialize a flag variable to True. b. For each character in the current string, check if it is not present in the allowed string, set the flag variable to False, and break the loop. c. If the flag variable is still True after checking all the characters, increment the count.

  3. Return the count.

Code:

class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for word in words: flag = True for char in word: if char not in allowed: flag = False break if flag: count += 1 return count

Time Complexity: O(n * m), where n is the number of words in the array and m is the maximum length of a word.

Space Complexity: O(1), as we are not using any extra space for storing the characters or strings.

Count The Number Of Consistent Strings Solution Code

1