✅3 Method's 🤯 || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥
\n# Intuition:\n\nThe Intuition is to determine if two strings are anagrams, compare the characters in both strings and check if they have the same characters b
\n# Intuition:\n\nThe 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. 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 1: Sorting\n# Explanation:\n\n1. `sort(s.begin(), s.end());` sorts the characters in string `s` in ascending order. This rearranges the characters in `s` so that they are in alphabetical order.\n2. `sort(t.begin(), t.end());` sorts the characters in string `t` in ascending order. Similarly, this rearranges the characters in `t` to be in alphabetical order.\n3. `return s == t;` compares the sorted strings `s` and `t` using the equality operator (`==`). If the sorted strings are equal, it means that the original strings `s` and `t` have the same characters in the same order, indicating that they are anagrams. In this case, the function returns `true`. Otherwise, if the sorted strings are not equal, the function returns `false`, indicating that the strings are not anagrams.\n\nThis code takes advantage of the fact that anagrams have the same characters, but in different orders. By sorting the characters, the code transforms the problem into a comparison of the sorted strings, simplifying the anagram check.\n\nHowever, it\'s worth noting that this approach has a time complexity of O(n log n) due to the sorting operation, where n is the length of the strings.\n\n# Code\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```Java []\nimport java.util.Arrays;\n\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        char[] sChars = s.toCharArray();\n        char[] tChars = t.toCharArray();\n        \n        Arrays.sort(sChars);\n        Arrays.sort(tChars);\n        \n        return Arrays.equals(sChars, tChars);\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        sorted_s = sorted(s)\n        sorted_t = sorted(t)\n        return sorted_s == sorted_t\n```\n\n# Approach 2: Hash Table\n# Explanation:\n1. Create an unordered map `count` to store the character frequencies. The key of the map represents a character, and the value represents its frequency.\n2. Iterate over each character `x` in string `s`. For each character, increment its frequency in the `count` map by using the `count[x]++` expression.\n3. Iterate over each character `x` in string `t`. For each character, decrement its frequency in the `count` map by using the `count[x]--` expression.\n4. Iterate over each pair `x` in the `count` map. Each pair consists of a character and its corresponding frequency. Check if any frequency (`x.second`) is non-zero. If any frequency is non-zero, it means there is a character that appears more times in one string than the other, indicating that the strings are not anagrams. In that case, return `false`.\n5. If all frequencies in the `count` map are zero, it means the strings `s` and `t` have the same characters in the same frequencies, making them anagrams. In this case, the function returns `true`.\n\nThis approach counts the frequency of characters in one string and then adjusts the count by decrementing for the other string. If the strings are anagrams, the frequency of each character will cancel out, resulting in a map with all zero frequencies.\n\nThe time complexity of this solution is O(n), where n is the total number of characters in both strings. It iterates over each character once to count the frequencies and then compares the frequencies in the map, making it an efficient solution for the problem.\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        unordered_map<char, int> count;\n        \n        // Count the frequency of characters in string s\n        for (auto x : s) {\n            count[x]++;\n        }\n        \n        // Decrement the frequency of characters in string t\n        for (auto x : t) {\n            count[x]--;\n        }\n        \n        // Check if any character has non-zero frequency\n        for (auto x : count) {\n            if (x.second != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n};\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        Map<Character, Integer> count = new HashMap<>();\n        \n        // Count the frequency of characters in string s\n        for (char x : s.toCharArray()) {\n            count.put(x, count.getOrDefault(x, 0) + 1);\n        }\n        \n        // Decrement the frequency of characters in string t\n        for (char x : t.toCharArray()) {\n            count.put(x, count.getOrDefault(x, 0) - 1);\n        }\n        \n        // Check if any character has non-zero frequency\n        for (int val : count.values()) {\n            if (val != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        count = defaultdict(int)\n        \n        # Count the frequency of characters in string s\n        for x in s:\n            count[x] += 1\n        \n        # Decrement the frequency of characters in string t\n        for x in t:\n            count[x] -= 1\n        \n        # Check if any character has non-zero frequency\n        for val in count.values():\n            if val != 0:\n                return False\n        \n        return True\n```\n\n# Approach 3: Hash Table (Using Array)\n# Code\n```C++ []\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        int count[26] = {0};\n        \n        // Count the frequency of characters in string s\n        for (char x : s) {\n            count[x - \'a\']++;\n        }\n        \n        // Decrement the frequency of characters in string t\n        for (char x : t) {\n            count[x - \'a\']--;\n        }\n        \n        // Check if any character has non-zero frequency\n        for (int val : count) {\n            if (val != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n};\n```\n```Java []\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        int[] count = new int[26];\n        \n        // Count the frequency of characters in string s\n        for (char x : s.toCharArray()) {\n            count[x - \'a\']++;\n        }\n        \n        // Decrement the frequency of characters in string t\n        for (char x : t.toCharArray()) {\n            count[x - \'a\']--;\n        }\n        \n        // Check if any character has non-zero frequency\n        for (int val : count) {\n            if (val != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        count = [0] * 26\n        \n        # Count the frequency of characters in string s\n        for x in s:\n            count[ord(x) - ord(\'a\')] += 1\n        \n        # Decrement the frequency of characters in string t\n        for x in t:\n            count[ord(x) - ord(\'a\')] -= 1\n        \n        # Check if any character has non-zero frequency\n        for val in count:\n            if val != 0:\n                return False\n        \n        return True\n```\n\n![CUTE_CAT.png](https://assets.leetcode.com/users/images/9c155cb0-77e1-4291-9042-d43d09545b85_1687848555.5456593.png)\n\n**If you are a beginner solve these problems which makes concepts clear for future coding:**\n1. [Two Sum](https://leetcode.com/problems/two-sum/solutions/3619262/3-method-s-c-java-python-beginner-friendly/)\n2. [Roman to Integer](https://leetcode.com/problems/roman-to-integer/solutions/3651672/best-method-c-java-python-beginner-friendly/)\n3. [Palindrome Number](https://leetcode.com/problems/palindrome-number/solutions/3651712/2-method-s-c-java-python-beginner-friendly/)\n4. [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/solutions/3666304/beats-100-c-java-python-beginner-friendly/)\n5. [Remove Element](https://leetcode.com/problems/remove-element/solutions/3670940/best-100-c-java-python-beginner-friendly/)\n6. [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/solutions/3672475/4-method-s-c-java-python-beginner-friendly/)\n7. [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/solutions/3675747/beats-100-c-java-python-beginner-friendly/)\n8. [Majority Element](https://leetcode.com/problems/majority-element/solutions/3676530/3-methods-beats-100-c-java-python-beginner-friendly/)\n9. [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3676877/best-method-100-c-java-python-beginner-friendly/)\n10. [Valid Anagram](https://leetcode.com/problems/valid-anagram/solutions/3687854/3-methods-c-java-python-beginner-friendly/)\n11. [Group Anagrams](https://leetcode.com/problems/group-anagrams/solutions/3687735/beats-100-c-java-python-beginner-friendly/)\n12. **Practice them in a row for better understanding and please Upvote for more questions.**\n\n\n**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**
['Hash Table', 'Sorting', 'C++', 'Java', 'Python3']