C++ | Hashing | O(n), 0ms, Beats 100% | Easy to Understand
EXPLANATION\n- Make two vectors of size 26 and initialize them with 0 for storing each character count of s and t strings respectively.\n\nvector<int> c1(26, 0)
**EXPLANATION**\n- Make **two vectors** of size **26** and *initialize* them with **0** for storing each **character count** of **```s```** and **```t```** strings respectively.\n```\nvector<int> c1(26, 0), c2(26, 0); // for storing character counts of \'s\' and \'t\' respectively\n```\n- Iterate both **```s```** and **```t```** strings and store each **character count** in vector **```c1```** and **```c2```** respectively.\n```\n// keep character count in s\nfor (char c: s) c1[c-\'a\'] ++;\n\n// keep character count in t\nfor (char c: t) c2[c-\'a\'] ++;\n```\n- Finally check if all the **character counts** in **```c1```** and **```c2```** are equal or not.\n\t- If **any of them** is **not equal** return *false*.\n\t- If **all** of them are **equal** then return *true*.\n```\nfor (int i = 0; i < 26; ++i) {\n\tif (c1[i] != c2[i]) \n\t\treturn false;\n}\nreturn true;\n```\n\n**COMPLETE CODE**\n```\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        vector<int> c1(26, 0), c2(26, 0); // for storing character counts of \'s\' and \'t\' respectively\n        \n        // keep character count in s\n        for (char c: s) c1[c-\'a\'] ++;\n        \n        // keep character count in t\n        for (char c: t) c2[c-\'a\'] ++;\n        \n        // check the character count of each characters, if any one is not equal then return false\n        for (int i = 0; i < 26; ++i) {\n            if (c1[i] != c2[i]) \n                return false;\n        }\n        return true;\n    }\n};\n```\n\n**TIME COMPLEXITY**\nHere, a = sizeof(s), b=sizeof(t), n=max(a,b)\n**O(a+b+26)=O(n)** [ *Since, traversing ```s``` and ```t``` takes **O(sizeof(s))** and **O(sizeof(t))** and traversing ```c1``` an ```c2``` takes **26** iterations* ]\n\n**SPACE COMPLEXITY**\n**O(26+26)=O(1)** [ *```c1``` and ```c2``` are of 26 size each* ]
['C']