3 Different Approaches | 700ms to 0ms 🔥| Beats 100% in both Time and Space 😎
Intuition\nThe given problem is to determine whether two strings, s and t, are anagrams of each other or not. An anagram is a word or phrase formed by rearrangi
# Intuition\nThe given problem is to determine whether two strings, `s` and `t`, are anagrams of each other or not. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The code uses an array to keep track of the frequency of each letter in both strings and then checks if the frequency arrays are equal.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize two arrays, `freqS` and `freqT`, of size 26 to represent the frequency of each lowercase English letter. Initialize all elements of both arrays to 0.\n2. Iterate through each character in string `s` and update the frequency array `freqS` accordingly.\n3. Iterate through each character in string `t` and update the frequency array `freqT` accordingly.\n4. Compare the frequency arrays `freqS` and `freqT` for each letter. If they are not equal, return false, indicating that the strings are not anagrams.\n5. If the loop completes without returning false, return true, indicating that the strings are anagrams.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n- Let n be the length of the input strings `s` and `t`. The code has three loops:\n- The first two loops iterate through each character in strings `s` and `t` once, each taking O(n) time.\n- The third loop iterates through the frequency arrays of size 26, which takes constant time.\nTherefore, the overall time complexity is O(n).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n- The code uses two arrays of size 26 to store the frequency of each letter. Therefore, the space complexity is O(1) since the size of the arrays is constant regardless of the input size.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code - 0ms\n```\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        int freqS[26] = {0};\n        int freqT[26] = {0};\n\n        for(auto x : s) freqS[x - \'a\']++;\n        for(auto x : t) freqT[x - \'a\']++;\n\n        for(int i = 0; i < 26; i++)\n        {\n            if(freqS[i] != freqT[i])\n                return false;\n        }\n        \n        return true;\n    }\n};\n```\n# Code - 12 ms\n```\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        sort(s.begin(),s.end());\n        sort(t.begin(),t.end());\n        return (s == t);\n    }\n};\n```\n# Code - 726ms\n```\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        \n        if(s.length() != t.length())\n            return false;\n        \n        int n = s.length();\n        for(int i = 0; i < n; i++)\n        {\n            char curr = s[i];\n            int index = t.find(curr);\n            if(index != string::npos)\n            t.erase(index,1);\n        }\n        if(t.length() > 0)\n            return false;\n        \n        return true;\n        \n    }\n};\n```\n![lc1.png](https://assets.leetcode.com/users/images/bc7c7f6e-682a-4292-a158-979fa3f9655f_1702700055.6121373.png)\n
['Hash Table', 'String', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']