✔️ Java 100% Faster | O(N) Solution | Explained
Complexity:\nTime: O(N)\nMemory: O(1)\n\n# Code\nRuntime: beats 100%\nMemory: beats 95%\n\nclass Solution {\n    public boolean isAnagram(String s, String t) {\
# Complexity:\nTime: O(N)\nMemory: O(1)\n\n# Code\nRuntime: beats 100%\nMemory: beats 95%\n```\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        int[] a = new int[26];\n        for (char c : s.toCharArray()) {\n            a[c - \'a\']++;\n        }\n        for (char c : t.toCharArray()) {\n            a[c - \'a\']--;\n        }\n        for (int n : a) {\n            if (n != 0) return false;\n        }\n        return true;\n    }\n}\n```\nExplanation:\n1. I create array of ints with size 26 for all lowercase letters.\n2. In the first loop I count occurences of letters in the first string by incrementing corresponding int in array. For example, if the letter (char) is \'b\', then \'b\' - \'a\' is equal to 1. So, I increment value with index 1: array[1]++.\n3. In the second loop I do the same, but decrement for each letter.\n4. In the third loop I make sure that all values in array are 0. If yes, then the answer is true. If not, then the given strings are not anagrams.
['Java']