Simple Java Code ☠️
Code\njava []\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        HashSet<Character> set = new HashSet<>();\n   
# Code\n```java []\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        HashSet<Character> set = new HashSet<>();\n        for(int i=0;i<allowed.length();i++){\n            set.add(allowed.charAt(i));\n        }\n        int count = 0,i=0,j=0;\n        for(i=0;i<words.length;i++){\n            String str = words[i];\n            for(j=0;j<str.length();j++){\n                if(!set.contains(str.charAt(j))){\n                    break;\n                }\n            }\n            if(j==str.length()){\n                count++;\n            }\n        }\n        return count;\n    }\n}\n```\n\n\n### Explanation:\n\n```java\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        HashSet<Character> set = new HashSet<>();\n        for(int i = 0; i < allowed.length(); i++){\n            set.add(allowed.charAt(i));\n        }\n```\n\n#### Step 1: Create a Set of Allowed Characters\n- **`HashSet<Character> set = new HashSet<>();`**: The set will store all the characters from the `allowed` string. \n  - **Reason**: A `HashSet` allows for fast lookup (O(1) on average), making it ideal for checking if characters in `words` are allowed.\n  \n- **`for(int i = 0; i < allowed.length(); i++){ set.add(allowed.charAt(i)); }`**:\n  - This loop iterates through the `allowed` string character by character and adds each character to the `HashSet`. \n  - After this loop, the set will contain all allowed characters.\n  \nExample:\n- If `allowed = "ab"`, then the set will contain `{\'a\', \'b\'}`.\n\n---\n\n```java\n        int count = 0, i = 0, j = 0;\n```\n#### Step 2: Initialize Counters\n- **`count = 0`**: This variable will store the number of consistent strings in the `words` array.\n- **`i` and `j`**: These are loop control variables that will be used to iterate over the `words` array and the characters of each word.\n\n---\n\n```java\n        for(i = 0; i < words.length; i++){\n            String str = words[i];\n            for(j = 0; j < str.length(); j++){\n                if(!set.contains(str.charAt(j))){\n                    break;\n                }\n            }\n            if(j == str.length()){\n                count++;\n            }\n        }\n```\n\n#### Step 3: Iterate Over Each Word and Check Consistency\n- **Outer loop (`for(i = 0; i < words.length; i++)`)**:\n  - This loop iterates over each string in the `words` array.\n  - **`String str = words[i];`**: For each iteration, the string `str` represents the current word from the `words` array.\n\n- **Inner loop (`for(j = 0; j < str.length(); j++)`)**:\n  - This loop iterates over each character in the current word (`str`).\n  - **`if(!set.contains(str.charAt(j)))`**: \n    - The code checks if the current character `str.charAt(j)` is in the `HashSet` of allowed characters.\n    - If the character is not allowed (i.e., it is not present in the set), the loop breaks, which means the current word is not consistent.\n\n- **After the inner loop (`if(j == str.length())`)**:\n  - If the inner loop runs through all the characters of the word without breaking, it means the word is consistent (all characters are in the set).\n  - **`count++`**: In this case, we increment the `count` variable to indicate that this word is consistent.\n\n#### Step 4: Return the Result\n```java\n        return count;\n    }\n}\n```\nFinally, the method returns the `count` of consistent strings.\n\n---\n\n### Example Walkthrough:\n\nLet\u2019s take an example to understand the flow:\n\n#### Input:\n- **`allowed = "abc"`**\n- **`words = ["a", "b", "c", "ab", "bc", "abc", "abcd"]`**\n\n1. **Step 1**: The `HashSet` will contain `{\'a\', \'b\', \'c\'}`.\n2. **Step 2**: Initialize `count = 0`.\n\n**Iteration over `words`:**\n\n- **`"a"`**: The character `\'a\'` is in the set, so the word is consistent. Increment `count` to 1.\n- **`"b"`**: The character `\'b\'` is in the set, so the word is consistent. Increment `count` to 2.\n- **`"c"`**: The character `\'c\'` is in the set, so the word is consistent. Increment `count` to 3.\n- **`"ab"`**: Both characters `\'a\'` and `\'b\'` are in the set, so the word is consistent. Increment `count` to 4.\n- **`"bc"`**: Both characters `\'b\'` and `\'c\'` are in the set, so the word is consistent. Increment `count` to 5.\n- **`"abc"`**: All characters `\'a\'`, `\'b\'`, and `\'c\'` are in the set, so the word is consistent. Increment `count` to 6.\n- **`"abcd"`**: The character `\'d\'` is not in the set, so the word is not consistent.\n\nFinal result: **`count = 6`**.\n\n### Complexity Analysis:\n\n- **Time Complexity**: \n  - Creating the `HashSet` takes **O(L)**, where `L` is the length of the `allowed` string.\n  - For each word in the `words` array, we check each character, resulting in a total time complexity of **O(W * S)**, where:\n    - `W` is the number of words in the `words` array.\n    - `S` is the average length of a word.\n  - Therefore, the total time complexity is **O(L + W * S)**.\n\n- **Space Complexity**: \n  - Storing the `HashSet` requires **O(L)** space, where `L` is the length of the `allowed` string.\n  - Other variables take constant space, so the space complexity is **O(L)**.\n\n\n\n
['Array', 'Hash Table', 'String', 'Counting', 'Java']