LeetCode solutions

1786. Count the Number of Consistent Strings

My accepted JavaScript solution to LeetCode problem 1786, Count the Number of Consistent Strings, running in 60ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 60ms
  • Memory 63.7MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 60ms, memory 63.7MB, accepted 2025-12-29.

javascript
/**
* @param {string} allowed
* @param {string[]} words
* @return {number}
*/
var countConsistentStrings = function(allowed, words) {
    const set = new Set(allowed);
    let count = 0;
    for (const word of words) {
        let valid = true;
        for (const c of word) {
            if (!set.has(c)) {
                valid = false;
                break;
            }
        }
        if (valid) count++;
    }
    return count;
};

Source