Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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","abc"] Output: 7 Explanation: All strings are consistent.
Example 3:
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] Output: 4 Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
Constraints:
1 <= words.length <= 1041 <= allowed.length <= 261 <= words[i].length <= 10allowed are distinct.words[i] and allowed contain only lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
"ab" ["ad","bd","aaab","baa","badab"]
"abc" ["a","b","c","ab","ac","bc","abc"]
"cad" ["cc","acd","b","ba","bac","bad","ac","d"]
count-pairs-of-similar-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1684: Count the Number of Consistent Strings
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
boolean[] s = new boolean[26];
for (char c : allowed.toCharArray()) {
s[c - 'a'] = true;
}
int ans = 0;
for (String w : words) {
if (check(w, s)) {
++ans;
}
}
return ans;
}
private boolean check(String w, boolean[] s) {
for (int i = 0; i < w.length(); ++i) {
if (!s[w.charAt(i) - 'a']) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1684: Count the Number of Consistent Strings
func countConsistentStrings(allowed string, words []string) (ans int) {
s := [26]bool{}
for _, c := range allowed {
s[c-'a'] = true
}
check := func(w string) bool {
for _, c := range w {
if !s[c-'a'] {
return false
}
}
return true
}
for _, w := range words {
if check(w) {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #1684: Count the Number of Consistent Strings
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
s = set(allowed)
return sum(all(c in s for c in w) for w in words)
// Accepted solution for LeetCode #1684: Count the Number of Consistent Strings
impl Solution {
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
let n = words.len();
let mut make = [false; 26];
for c in allowed.as_bytes() {
make[(c - b'a') as usize] = true;
}
let mut ans = n as i32;
for word in words.iter() {
for c in word.as_bytes().iter() {
if !make[(c - b'a') as usize] {
ans -= 1;
break;
}
}
}
ans
}
}
// Accepted solution for LeetCode #1684: Count the Number of Consistent Strings
function countConsistentStrings(allowed: string, words: string[]): number {
const set = new Set([...allowed]);
const n = words.length;
let ans = n;
for (const word of words) {
for (const c of word) {
if (!set.has(c)) {
ans--;
break;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.