Beats 98% | Hashing using Array | Easy to Understand 🚀✅
Intuition\n Describe your first thoughts on how to solve this problem. \n1. The Intuition is to determine if two strings are anagrams, compare the characters in
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. The Intuition is to determine if two strings are anagrams, compare the characters in both strings and check if they have the same characters but in a different order \n2. By tracking the count of each character, if the counts match for all characters, the strings are anagrams; otherwise, they are not\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create an array of size 26 to store the character frequencies.\n2. Iterate over each character i in string s. For each character, increment its frequency in the array map by using the map[i-\'a\']++ expression.\n3. Iterate over each character i in string t. For each character, decrement its frequency in the array map by using the map[i-\'a\']-- expression, if there comes any character whose value is zero in map then return false else return true.\n\nIf the strings are anagrams, the frequency of each character will cancel out, resulting in a map with all zero frequencies.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) , linear time complexity ,where n is the length of string\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1), constant space\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        int n = s.length();\n        int m = t.length();\n\n        if (n != m) return false;\n\n        int map[26] = {0};\n\n        // Update character frequencies for both strings in a single loop\n        for (int i = 0; i < n; ++i) {\n            map[s[i] - \'a\']++;\n            map[t[i] - \'a\']--;\n        }\n\n        // Check if all frequencies are zero, indicating an anagram\n        for (int i :map) {\n            if (i != 0) return false;\n        }\n\n        return true;\n    }\n};\n\n```\n```javascript []\nclass Solution {\n    isAnagram(s, t) {\n        const n = s.length;\n        const m = t.length;\n        if (n !== m) {\n            return false;\n        }\n\n        const charCount = Array(26).fill(0);\n\n        for (const char of s) {\n            charCount[char.charCodeAt(0) - \'a\'.charCodeAt(0)] += 1;\n        }\n\n        for (const char of t) {\n            if (charCount[char.charCodeAt(0) - \'a\'.charCodeAt(0)] < 1) {\n                return false;\n            }\n            charCount[char.charCodeAt(0) - \'a\'.charCodeAt(0)] -= 1;\n        }\n\n        return true;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        n, m = len(s), len(t)\n        if n != m:\n            return False\n\n        char_count = [0] * 26\n\n        for char in s:\n            char_count[ord(char) - ord(\'a\')] += 1\n\n        for char in t:\n            if char_count[ord(char) - ord(\'a\')] < 1:\n                return False\n            char_count[ord(char) - ord(\'a\')] -= 1\n\n        return True\n\n```\n```Java []\npublic class Solution {\n    public boolean isAnagram(String s, String t) {\n        int n = s.length();\n        int m = t.length();\n\n        // If the lengths are not equal, the strings cannot be anagrams\n        if (n != m) {\n            return false;\n        }\n\n        // Initialize an array to store the frequency of each character in the first string\n        int[] charCount = new int[26];\n\n        // Count the frequency of each character in the first string\n        for (char c : s.toCharArray()) {\n            charCount[c - \'a\']++;\n        }\n\n        // Check the frequency of each character in the second string\n        for (char c : t.toCharArray()) {\n            // If a character in the second string is not present in the first string,\n            // or its frequency is already zero, the strings are not anagrams\n            if (charCount[c - \'a\'] < 1) {\n                return false;\n            }\n\n            // Decrement the frequency of the character in the array\n            charCount[c - \'a\']--;\n        }\n\n        // If all characters in the second string are accounted for in the first string,\n        // and the frequencies match, the strings are anagrams\n        return true;\n    }\n}\n\n```\n\n![87bab3dd-fabf-4b71-9ac5-984d9015438e_1678160362.0612018.jpeg](https://assets.leetcode.com/users/images/ef01375d-5e45-43bb-bb4c-3ee8008259a5_1702688036.7411647.jpeg)\n
['C++']