✅☑️ Best C++ 2 Solution Ever || Hash Table || Sorting || String || One Stop Solution.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can solve this question using Multiple Approaches. (Here I have explained all the po
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can solve this question using Multiple Approaches. (Here I have explained all the possible solutions of this problem).\n\n1. Solved using String + Sorting.\n2. Solved using String + Hash Table.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can easily understand the all the approaches by seeing the code which is easy to understand with comments.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nTime complexity is given in code comment.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSpace complexity is given in code comment.\n\n# Code\n```\n/*\n\n    Time Complexity : O(NlogN + MlogM), Sorting the String(s and t) costs O(NlogN + MlogM). Where N is the size\n    of the String(s) and M is the size of the String(t).\n\n    Space Complexity : O(1), Constant Space.\n\n    Solved using String + Sorting.\n\n*/\n\n\n/***************************************** Approach 1 *****************************************/\n\nclass Solution {            \npublic:\n    bool isAnagram(string s, string t) {\n        if(s.length() != t.length()){\n            return false;\n        }\n        sort(s.begin(), s.end());\n        sort(t.begin(), t.end());\n        if(s == t){\n            return true;  \n        }\n        return false;\n    }\n}; \n\n\n\n\n\n\n/*\n\n    Time Complexity : O(N), as we iterate the string to count the frequency of every character where N is the\n    length of the string.\n\n    Space Complexity : O(1), Constant space. As we are using  26 size-frequency array to store the frequency of\n    every character which is constant.\n\n    Solved using String + Hash Table.\n\n*/\n\n\n/***************************************** Approach 2 *****************************************/\n\nclass Solution {               \npublic:\n    bool isAnagram(string s, string t) {\n        if(s.length() != t.length()){\n            return false;\n        }\n        int arr[26] = {0};\n        for(int i=0; i<s.length(); i++){\n            arr[s[i]-\'a\']++;\n            arr[t[i]-\'a\']--;\n        }\n        for(int i=0; i<26; i++){\n            if(arr[i] != 0){\n                return false;\n            }\n        }\n        return true;\n    }\n};\n\n```\n\n***IF YOU LIKE THE SOLUTION THEN PLEASE UPVOTE MY SOLUTION BECAUSE IT GIVES ME MOTIVATION TO REGULARLY POST THE SOLUTION.***\n\n![WhatsApp Image 2023-02-10 at 19.01.02.jpeg](https://assets.leetcode.com/users/images/0a95fea4-64f4-4502-82aa-41db6d77c05c_1676054939.8270252.jpeg)
['Hash Table', 'String', 'Sorting', 'C++']