Frequency Count vs 1-line||0ms Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nFrequency Count. No hash\n# Approach\n Describe your approach to solving the problem. \
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFrequency Count. No hash\n# Approach\n<!-- Describe your approach to solving the problem. -->\ns is anagram of t $\\iff$ `freq(s)==freq(t)`\nIn other words, s is a permutation of t.\n \nC/C++/Python codes are implemented. The revised C code uses 1 loop by the suggestion of @Sergei which is fast, runs in 0ms & beats 100%.\n\n[Please turn on English subtitles if necessary]\n[https://youtu.be/DDSI3xzWCto?si=DmIRBD8beeKv5a98](https://youtu.be/DDSI3xzWCto?si=DmIRBD8beeKv5a98)\n\nPython 1 line code uses `sorted` with TC O(n log n)\nC++ 1 line code uses `multiset<char>` with TC O(n log n)\nThanks to @wawaroutas mentioning `std::ranges::is_permutation`, a similar C++ 1-line code is provided.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(26)$$\n#  Both of C &C++ Codes run 0ms Beat 100%\n```C++ []\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    vector<int> freq(string& s){\n        vector<int> ans(26, 0);\n        for(char c: s){\n            ans[c-\'a\']++;\n        }\n        return ans;\n    }\n    bool isAnagram(string s, string t) {\n        return freq(s)==freq(t);\n    }\n};\n```\n```python []\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        def freq(s):\n            ans=[0]*26\n            for c in s:\n                ans[ord(c)-ord(\'a\')]+=1\n            return ans\n\n        return freq(s)==freq(t)\n        \n```\n```C []\n#pragma GCC optimize("O3", "unroll-loops")\nbool isAnagram(char* s, char* t) {\n    int f[26]={0};\n    register int i;\n    for(i=0; s[i]!=\'\\0\' && t[i]!=\'\\0\'; i++){\n        f[s[i]-\'a\']++; \n        f[t[i]-\'a\']--;\n    }\n    if (s[i]!=\'\\0\' || t[i]!=\'\\0\') \n        return 0;\n    for(register i=0; i<26; i++)\n        if (f[i]!=0) return 0;\n    return 1;\n}\n```\n # Python 1 line using sorted TC O(n log n)\n```\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        return sorted(s)==sorted(t)\n        \n```\n# C++ 1 line using multiset<char>\n```\nclass Solution {\npublic:\n    bool isAnagram(string& s, string& t) {\n        return multiset<char>(s.begin(), s.end())==multiset<char>(t.begin(), t.end());\n    }\n};\n```\n# C++ 1 line using is_permutation\n```\nclass Solution {\npublic:\n    bool isAnagram(string& s, string& t) {\n        return is_permutation(s.begin(), s.end(), t.begin(), t.end());\n    }\n};\n```\n\n
['Array', 'C', 'C++', 'Python3']