Skip to main content
Back to problems
#1684
Easy Algorithms

Count the number of consistent strings

Array Hash Table String Bit Manipulation Counting
88.5% acceptance
Feb 25, 2026
2269
90
You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters appear in allowed. Return the number of consistent strings in the array words.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
    let mask: u32 = allowed.bytes().fold(0u32, |acc, b| acc | (1 << (b - b'a')));
    words
      .iter()
      .filter(|w| w.bytes().all(|b| mask >> (b - b'a') & 1 == 1))
      .count() as i32
  }
}