2 Approach (solutions) | Fully Explained 💯 | Simple and Efficient Code 💯🔥
1. (Set and Reduce) Approach\n\n# Intuition\nWe\'re counting the number of words in an array that consist only of characters from a given set of allowed charact
# **1. (Set and Reduce) Approach**\n\n# Intuition\nWe\'re counting the number of words in an array that consist only of characters from a given set of allowed characters.\n# Approach\n1. Convert the allowed characters into a set for quick lookup.\n2. For each word in the words array, split the word into individual characters.\n3. Check if every character in the word exists in the allowed set using the every function.\n4. If all characters are allowed, increment the count.\n5. Return the final count.\n# Complexity\n- Time complexity: `O(n * m)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(m)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nvar countConsistentStrings = function (allowed, words) {\n  allowed = new Set(allowed);\n\n  return words.reduce((count, word) => {\n    return word.split(\'\').every(l => allowed.has(l)) ? ++count : count;\n  }, 0);\n};\n\n```\n\n---\n\n# **2. (Brute Force) Approach**\n\n# Intuition\nWe\'re counting the number of words in an array that only contain characters from a given set of allowed characters.\n# Approach\n1. Initialize a count variable to keep track of the consistent words.\n2. For each word in the words array:\n    - Initialize a consistent flag as true.\n    - For each letter in the word:\n        - Check if the letter is not included in the allowed        characters using the includes function.\n        - If not allowed, set consistent to false and break out of the loop.\n    - If consistent is still true, increment the count.\n3. Return the final count.\n# Complexity\n- Time complexity: `O(n * m)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(1)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nvar countConsistentStrings = function (allowed, words) {\n  let count = 0;\n\n  for (let word of words) {\n    let consistent = true;\n    for (let letter of word) {\n      if (!allowed.includes(letter)) {\n        consistent = false;\n        break;\n      }\n    }\n    consistent ? count++ : \'\';\n  }\n\n  return count;\n};\n\n```\n\n\n---\n\n**Given the analysis, while this approach works, it has a higher time complexity compared to the first approach using `Set and reduce`. Therefore, if performance is a concern, the first approach is generally preferred. \uD83D\uDC4D**\n\nIf any doubts comment it.. \u2728\n\nHappy Coding :)\n\n![upvote.jpeg](https://assets.leetcode.com/users/images/770195a1-d985-4304-ac77-72768cc8d94b_1692887191.8325512.jpeg)\n
['Array', 'Hash Table', 'String', 'Bit Manipulation', 'JavaScript']