☕Java Solution | 1ms Beats 100%🔥 | Using Extra One Array ✅ | Easy to Understand 😎
Intuition\n Describe your first thoughts on how to solve this problem. \nBy seeing the problem statement, i thought of the best one HashMap :)\nTo store the fre
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy seeing the problem statement, i thought of the best one HashMap :)\nTo store the frequency of one string and comparing that with another string frequecny. However we can also minimize the extra computation of importing and implementing hashmap by using an array.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe know that the characters are english lowercase letters only. Therefore we make an array of length `26` to store frequency. As total 26 letters are there.\nNow we know that anagram means having same frequency of each and every letters and there difference is permutation of those letters.\n\nFor example\n```\ns = \'abc\'\nb = \'cba\'\n```\nboth having same frequency of each letters but their permutation is different in both the string.\n\nSo my apporach is simple. First by iterating one string, we will store the frequency of each letters in the array.\nWe got : \n```\nalphas = [1,1,1,0,0,0,0,.....,0]; # total length 26\n```\nnow, we have the same frequency of letters in both the string.\nBy iterating another string we will decrement the frequency.\nwe got :\n```\nalphas = [0,0,0,0,0,0,0,......,0] # total length 26\n```\n\nNow if the both are amagrams , we must get the elements of array to `0` in the because same frequency of letters exists in both string.\n\nSo now we iterate through the array and check if element equals `0` then return `true`. Otherwise return `false`.\n\n**Note :** `ch-\'a\'` gives the index because `if ch == \'b\', then \'b\' - \'a\' = 1` which we will consider as index of \'b\'.\n# Complexity\n- Time complexity: O(n)\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)$$ -->\nIt is constant bacuse we use an constant size of array (26).\n# Code\n```\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        int alphas[] = new int[26];\n        for(char ch : s.toCharArray()){\n            alphas[ch-\'a\']++;\n        }\n        for(char ch : t.toCharArray()){\n            alphas[ch-\'a\']--;\n        }\n        for(int i : alphas){\n            if(i != 0){\n                return false;\n            }\n        }\n        return true;\n    }\n}\n```\nIf any suggestion or doubt, Please comment :)\n\nPLEASE DO UPVOTE!!!\n\nThank You\n
['Array', 'Java']