🔥Easy Solutions in Java 📝, Python 🐍, JavaScript 🖥️and C++ 🧐Look at once 💻
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve the anagram problem, we can count the frequency of each letter in both strings
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve the anagram problem, we can count the frequency of each letter in both strings and compare them. If the frequencies of all letters are the same in both strings, then the two strings are anagrams of each other.\n\n# Approach\nWe can use an integer array `freq` of length 26 to keep track of the frequency of each letter in the two strings. We first check if the length of both strings is the same. If not, we return false because anagrams must have the same length. We loop through each character in both strings and increment the frequency of the corresponding letter in `s` and decrement the frequency of the corresponding letter in `t`.\nFinally, we loop through the `freq` array and check if all requencies are zero. If not, we return false. If all frequencies are zero, we return true because both strings are anagrams of each other.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the length of the strings. We need to loop through each character in both strings once to count the frequency of each letter, and then loop through the `freq` array once to check if all frequencies are zero.\n\n- Space complexity: $$O(1)$$, because the size of the `freq` array is fixed at 26, which is a constant value. Therefore, the space required to store the `freq` array does not depend on the length of the strings.\n\n\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n# Code\n``` java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        if (s.length() != t.length()) {\n            return false;\n        }\n        \n        int[] freq = new int[26];\n        for (int i = 0; i < s.length(); i++) {\n            freq[s.charAt(i) - \'a\']++;\n            freq[t.charAt(i) - \'a\']--;\n        }\n        \n        for (int i = 0; i < freq.length; i++) {\n            if (freq[i] != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n\n```\n``` Python []\nclass Solution(object):\n    def isAnagram(self, s, t):\n        """\n        :type s: str\n        :type t: str\n        :rtype: bool\n        """\n        if len(s) != len(t):\n            return False\n        \n        freq = [0] * 26\n        for i in range(len(s)):\n            freq[ord(s[i]) - ord(\'a\')] += 1\n            freq[ord(t[i]) - ord(\'a\')] -= 1\n        \n        for i in range(len(freq)):\n            if freq[i] != 0:\n                return False\n        \n        return True\n\n```\n``` C++ []\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        if (s.length() != t.length()) {\n            return false;\n        }\n        \n        vector<int> freq(26, 0);\n        for (int i = 0; i < s.length(); i++) {\n            freq[s[i] - \'a\']++;\n            freq[t[i] - \'a\']--;\n        }\n        \n        for (int i = 0; i < freq.size(); i++) {\n            if (freq[i] != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n};\n```\n``` JavaScript []\nvar isAnagram = function(s, t) {\n    if (s.length !== t.length) {\n        return false;\n    }\n    \n    var freq = new Array(26).fill(0);\n    for (var i = 0; i < s.length; i++) {\n        freq[s.charCodeAt(i) - \'a\'.charCodeAt(0)]++;\n        freq[t.charCodeAt(i) - \'a\'.charCodeAt(0)]--;\n    }\n    \n    for (var i = 0; i < freq.length; i++) {\n        if (freq[i] !== 0) {\n            return false;\n        }\n    }\n    \n    return true;\n};\n\n```\n\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```
['String', 'Python', 'C++', 'Java', 'JavaScript']