1684. Count the Number of Consistent Strings
EasyView on LeetCode
Time: O(n*m)
Space: O(1)
Problem Overview
Count the Number of Consistent Strings (Easy) asks you to solve a structured algorithmic task. This is a common Array / Hash Table pattern in coding interviews. Boolean set of allowed chars; count words where every char is in set.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Boolean set of allowed chars; count words where every char is in set.
Related patterns: Array, Hash Table, String
1684.cs
C#
// Approach: Boolean set of allowed chars; count words where every char is in set.
// Time: O(n*m) Space: O(1)
public class Solution
{
public int CountConsistentStrings(string allowed, string[] words)
{
bool[] s = new bool[26];
foreach (char c in allowed)
s[c - 'a'] = true;
int ans = 0;
foreach (string w in words)
{
if (Check(w, s))
ans++;
}
return ans;
}
private bool Check(string w, bool[] s)
{
for (int i = 0; i < w.Length; i++)
{
if (!s[w[i] - 'a'])
return false;
}
return true;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 12. Integer to Roman(Medium)
- 13. Roman to Integer(Easy)
- 14. Longest Common Prefix(Easy)
- 15. 3Sum(Medium)