GOLANG optimized with followup - O(n)
This problem can be solved by sorting the characters, but sorting is computationally expensive. We want a solution that loops once over each string. We are goin
This problem can be solved by sorting the characters, but sorting is computationally expensive. We want a solution that loops once over each string. We are going to do the following: keep a slice of integers representing the amount of appearances of each of the 26 lowercase English letters:\n```\nchars := make([]int, 26)\n```\nWe loop once over s, incrementing the relevant number. We want chars[0] to refer to \'a\', chars[1] to refer to \'b\', etc.\n```\nfor _, v := range s {\n\ti := int(v - \'a\')\n\tchars[i]++\n}\n```\nThen we do the same over t. We can maintain another slice, but we also can just decrement towards 0 again:\n```\nfor _, v := range t {\n\ti := int(v - \'a\')\n\tchars[i]--\n}\n```\nNow, if chars contains only zeros, we return true, otherwise false. This operation is O(1).\n```\nfor _, v := range chars {\n\tif v != 0 {\n\t\treturn false\n\t}\n}\n\nreturn true\n```\n\nFull solution:\n```\nfunc isAnagram(s string, t string) bool {\n    chars := make([]int, 26)\n    \n    for _, v := range s {\n\t\ti := int(v - \'a\')\n\t\tchars[i]++\n\t}\n    \n    for _, v := range t {\n\t\ti := int(v - \'a\')\n\t\tchars[i]--\n\t}\n    \n\tfor _, v := range chars {\n\t\tif v != 0 {\n\t\t\treturn false\n\t\t}\n\t}\n    \n    return true\n}\n```\n\n**Bonus:** What if the inputs contain Unicode characters?\n\nWe could maybe go on a quest to find out how many Unicode chars are there, and maintain a slice with many thousands of elements, and technically this would still be O(n), but it is a cheeky solution with a lot of waste. We can do better, we can use a map:\n```\nchars := make(map[rune]int)\n```\nWe need to make a map of [runes](https://go.dev/blog/strings), because bytes are not adequae for all unicode characters. The rest is easy.\nFull code:\n```\nfunc isAnagram(s string, t string) bool {\n    chars := make(map[rune]int)\n    \n    for _, v := range s {\n        chars[v]++\n    }\n    \n    for _, v := range t {\n        chars[v]--\n    }\n    \n    for _, v := range chars {\n        if v != 0 {\n            return false\n        }\n    }\n    \n    return true\n}\n```\nAlternative version with a couple tricks added:\n```\nfunc isAnagram(s string, t string) bool {\n    chars := make(map[rune]int)\n    \n    for _, v := range s {\n        chars[v]++\n    }\n    \n    for _, v := range t {\n        if number, exists := chars[v]; !exists || number == 0 {\n            return false\n        }\n        chars[v]--\n    }\n    \n    return len(s) == len(t)\n}\n```\nIf we are going to decrement and we find that the char count is already 0, or that we don\'t even have a count, then we can say that the second string has more of that character than the first, and therefore we return false immediately.\nIf we never find ourselves in that situation, then the first string has more or equal of every character than the second string. We can quickly check if it is **sometimes more** or if it is **always equal** by checking the total lengths.
['Go']