Skip to content

1684. Count the Number of Consistent Strings 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  int countConsistentStrings(string allowed, vector<string>& words) {
    return accumulate(words.begin(), words.end(), 0,
                      [&allowed](int subtotal, const string& word) {
      return subtotal + ranges::all_of(word, [&allowed](char c) {
        return allowed.find(c) != string::npos;
      });
    });
  }
};
1
2
3
4
5
6
7
class Solution {
  public int countConsistentStrings(String allowed, String[] words) {
    return (int) Arrays.stream(words)
        .filter(word -> word.matches(String.format("[%s]*", allowed)))
        .count();
  }
}
1
2
3
4
class Solution:
  def countConsistentStrings(self, allowed: str, words: list[str]) -> int:
    return sum(all(c in allowed for c in word)
               for word in words)