[Java / Kotlin / Python] Set
A string is consistent if all characters in the string appear in the string allowed.\n\n# Explanations to a child\nStrings "aaab" and "baa" are consistent to th
A string is `consistent` if all characters in the string appear in the string allowed.\n\n# Explanations to a child\nStrings "aaab" and "baa" are consistent to the allowed string "ab" since they only contain characters \'a\' and \'b\'.\nWe don\'t need to look at the occurrences of chars.\nA string is not consistent if at least one char is not in the string allowed. The search of a char in a string is linear O(n), find a way to make the search constant O(1).\nLet\'s transform the `allowed` into a Set or array. The array of size is 26 because all chars are lowercase English letters.\nPersonally I prefer a Set over array, Set works for all cases.\n\n\n**Java**\n\n```\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        Set<Character> allowedSet = new HashSet<>();\n        for(int i=0;i<allowed.length();i++){\n            allowedSet.add(allowed.charAt(i));\n        }\n        \n        int count = 0;\n        for(String word: words){\n            if(isConsistent(allowedSet, word)) count++;\n        }\n        \n        return count;\n    }\n    \n    boolean isConsistent(Set<Character> allowedSet, String word){\n        for (int i = 0; i < word.length(); i++){\n            if(!allowedSet.contains(word.charAt(i))) return false;   \n        }\n        return true;\n    }\n}\n```\n\n**Kotlin**\n\n```\nclass Solution {\n    \n    fun countConsistentStrings(allowed: String, words: Array<String>): Int {\n        val allowedSet = mutableSetOf<Char>()\n        for (c in allowed) {\n            allowedSet.add(c)\n        }\n        \n        var count = 0\n        for (word in words) {\n            if (isConsistent(allowedSet, word)) {\n                count++\n            }\n\n        }\n        return count\n    }\n\n    private fun isConsistent(allowedSet: Set<Char>, word: String): Boolean {\n        for (w in word) {\n            if (!allowedSet.contains(w)) {\n                return false\n            }\n        }\n        return true\n    }\n\n}\n```\n\n**Python**\n```\nclass Solution:\n    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:\n        allowed_set = set(allowed)\n        count = 0\n        for word in words:\n            if all(c in allowed_set for c in word):\n                count+= 1\n        return count\n```
['Ordered Set', 'Python', 'Java', 'Kotlin']