💯Faster✅💯 Lesser✅4 Methods🔥Sorting🔥Hash Table🔥Character Array🔥Counter🔥Python🐍Java☕C++✅C📈
\uD83D\uDE80 Hi, I\'m Mohammed Raziullah Ansari, and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Proble
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe `Valid Anagram` problem typically asks us to determine whether two given strings are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another. In this context, an anagram of a string is another string with the same characters, but possibly in a different order.\n\n# \uD83D\uDD0D Methods To Solve This Problem:\nI\'ll be covering four different methods to solve this problem:\n1. Sorting\n2. Hash Table\n3. Character Array\n4. Counter\n\n# 1. Sorting: \n- Sort both strings.\n- Compare the sorted strings to check if they are equal.\n# Complexity\n* \u23F1\uFE0F Time Complexity: `O(n log n) `- Due to the sorting operation.\n\n* \uD83D\uDE80 Space Complexity: `O(1)` - No extra space used other than the input strings.\n\n# Code\n```Python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        return sorted(s) == sorted(t)\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        char[] charArrayS = s.toCharArray();\n        char[] charArrayT = t.toCharArray();\n        Arrays.sort(charArrayS);\n        Arrays.sort(charArrayT);\n        return Arrays.equals(charArrayS, charArrayT);\n    }\n}\n```\n```C++ []\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# 2. Hash Table:\n- Create a hash table (dictionary) to store the frequency of characters in the first string.\n- Iterate through the second string and decrement the corresponding frequency in the hash table.\n- If the hash table becomes empty, the strings are anagrams.\n# Complexity\n* \u23F1\uFE0F Time Complexity: `O(n)` - Iterate through both strings.\n\n* \uD83D\uDE80 Space Complexity: `O(n)` - Space required for the hash table.\n\n# Code\n```Python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        if len(s) != len(t):\n            return False\n        char_count = {}\n        for char in s:\n            char_count[char] = char_count.get(char, 0) + 1\n        for char in t:\n            if char_count.get(char, 0) == 0:\n                return False\n            char_count[char] -= 1\n        return True\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        if (s.length() != t.length()) {\n            return false;\n        }\n        Map<Character, Integer> charCount = new HashMap<>();\n        for (char c : s.toCharArray()) {\n            charCount.put(c, charCount.getOrDefault(c, 0) + 1);\n        for (char c : t.toCharArray()) {\n            if (charCount.getOrDefault(c, 0) == 0) {\n                return false;\n            }\n            charCount.put(c, charCount.get(c) - 1);\n        }\n        return true;\n    }\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        unordered_map<char, int> charCount;\n        for (char c : s) {\n            charCount[c]++;\n        }\n        for (char c : t) {\n            if (charCount[c] == 0) {\n                return false;\n            }\n            charCount[c]--;\n        }\n        return true;\n    }\n};\n```\n```C []\nbool isAnagram(char* s, char* t) {\n    if (strlen(s) != strlen(t)) {\n        return false;\n    }\n    int len = strlen(s);\n    int* charCountS = (int*)calloc(26, sizeof(int));\n    int* charCountT = (int*)calloc(26, sizeof(int));\n    for (int i = 0; i < len; i++) {\n        charCountS[s[i] - \'a\']++;\n    }\n    for (int i = 0; i < len; i++) {\n        charCountT[t[i] - \'a\']++;\n    }\n    bool result = true;\n    for (int i = 0; i < 26; i++) {\n        if (charCountS[i] != charCountT[i]) {\n            result = false;\n            break;\n        }\n    }\n    free(charCountS);\n    free(charCountT);\n    return result;\n}\n```\n# 3. Character Array:\n- Create two arrays of size 26 to represent the count of each character in the alphabet for both strings.\n- Iterate through both strings, updating the count in the arrays.\n- Compare the arrays to check if they are equal.\n# Complexity\n* \u23F1\uFE0F Time Complexity: `O(n)` - Iterate through both strings.\n\n* \uD83D\uDE80 Space Complexity: `O(1)` - Fixed-size character arrays.\n\n# Code\n```Python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        if len(s) != len(t):\n            return False\n        char_count_s = [0] * 26\n        char_count_t = [0] * 26\n        for char in s:\n            char_count_s[ord(char) - ord(\'a\')] += 1\n        for char in t:\n            char_count_t[ord(char) - ord(\'a\')] += 1\n        return char_count_s == char_count_t\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        if (s.length() != t.length()) {\n            return false;\n        }\n        int[] charCountS = new int[26];\n        int[] charCountT = new int[26];\n        for (char c : s.toCharArray()) {\n            charCountS[c - \'a\']++;\n        }\n        for (char c : t.toCharArray()) {\n            charCountT[c - \'a\']++;\n        }\n        return Arrays.equals(charCountS, charCountT);\n    }\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        int charCountS[26] = {0};\n        int charCountT[26] = {0};\n        for (char c : s) {\n            charCountS[c - \'a\']++;\n        }\n        for (char c : t) {\n            charCountT[c - \'a\']++;\n        }\n        return memcmp(charCountS, charCountT, sizeof(charCountS)) == 0;\n    }\n};\n```\n```C []\nbool isAnagram(char* s, char* t) {\n    if (strlen(s) != strlen(t)) {\n        return false;\n    }\n    int len = strlen(s);\n    int charCountS[26] = {0};\n    int charCountT[26] = {0};\n    for (int i = 0; i < len; i++) {\n        charCountS[s[i] - \'a\']++;\n    }\n    for (int i = 0; i < len; i++) {\n        charCountT[t[i] - \'a\']++;\n    }\n    return memcmp(charCountS, charCountT, sizeof(charCountS)) == 0;\n}\n```\n# 4. Counter:\n- Use the Counter class from the collections module to get character counts for both strings.\n- Compare the Counters to check if they are equal.\n# Complexity\n* \u23F1\uFE0F Time Complexity: `O(n)` - The Counter class internally iterates through the strings. \n\n* \uD83D\uDE80 Space Complexity: `O(n)` - Space required for the Counters.\n\n# Code\n```Python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        return Counter(s) == Counter(t)\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        Map<Character, Integer> counterS = new HashMap<>();\n        Map<Character, Integer> counterT = new HashMap<>();\n        for (char c : s.toCharArray()) {\n            counterS.put(c, counterS.getOrDefault(c, 0) + 1);\n        }\n        for (char c : t.toCharArray()) {\n            counterT.put(c, counterT.getOrDefault(c, 0) + 1);\n        }\n        return counterS.equals(counterT);\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        unordered_map<char, int> counterS;\n        unordered_map<char, int> counterT;\n        for (char c : s) {\n            counterS[c]++;\n        }\n        for (char c : t) {\n            counterT[c]++;\n        }\n        return counterS == counterT;\n    }\n};\n```\n```C []\ntypedef struct {\n    char key;\n    int value;\n} KeyValuePair;\n\nbool isAnagram(char* s, char* t) {\n    int len_s = strlen(s);\n    int len_t = strlen(t);\n    if (len_s != len_t) {\n        return false;\n    }\n    KeyValuePair* counterS = (KeyValuePair*)malloc(26 * sizeof(KeyValuePair));\n    KeyValuePair* counterT = (KeyValuePair*)malloc(26 * sizeof(KeyValuePair));\n\n    for (int i = 0; i < 26; i++) {\n        counterS[i].key = i + \'a\';\n        counterS[i].value = 0;\n        counterT[i].key = i + \'a\';\n        counterT[i].value = 0;\n    }\n    for (int i = 0; i < len_s; i++) {\n        counterS[s[i] - \'a\'].value++;\n    }\n    for (int i = 0; i < len_t; i++) {\n        counterT[t[i] - \'a\'].value++;\n    }\n    bool result = memcmp(counterS, counterT, 26 * sizeof(KeyValuePair)) == 0;\n    free(counterS);\n    free(counterT);\n    return result;\n}\n```\n# \uD83C\uDFC6Conclusion: \nHash table approach is a good balance between time and space complexity. It has a time complexity of `O(n)` and a space complexity of `O(n)`, where n is the length of the input string.\n\n# \uD83D\uDCA1 I invite you to check out my profile for detailed explanations and code for each method. Happy coding and learning! \uD83D\uDCDA
['Array', 'Hash Table', 'String', 'C', 'Sorting', 'C++', 'Java', 'Python3']