Prime numbers approach - beats 99.80% Runtime and 92.5% Memory
Intuition\nPrime numbers are only divisible by themselves. If you multiply them together, you can only reach the resulting number by using the same values. If y
# Intuition\nPrime numbers are only divisible by themselves. If you multiply them together, you can only reach the resulting number by using the same values. If you map the chars to prime numers and the resulting total is equal, it must be comprised of the same chars in the same amount. Meaning it is an anagram.\n\n# Approach\n- Iterate through each string and convert to prime number.\n- Add to new array if the total doesn\'t already exist, else add to existing array.\n\n![image.png](https://assets.leetcode.com/users/images/44af4b64-a7bc-4b5c-a98c-a661a3ea9323_1683232250.9728827.png)\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nconst primes: number[] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101];\n\nfunction convertStringToPrimeValue(s: string): number {\n    let total: number = 1;\n    for (let i = 0; i < s.length; i++) {\n        const c = s[i];\n        const primeNumber = primes[c.charCodeAt(0) - 97];\n        total *= primeNumber;\n    }\n    return total;\n}\n\nfunction groupAnagrams(strs: string[]): string[][] {\n    const groupedAnagrams: string[][] = [];\n    const groupAnagramMap: Map<number, number> = new Map();\n    for (let i = 0; i < strs.length; i++) {\n        const s = strs[i];\n        const primeValue: number = convertStringToPrimeValue(s);\n        if (groupAnagramMap.has(primeValue)) {\n            groupedAnagrams[groupAnagramMap.get(primeValue)].push(s);\n        } else {\n            groupAnagramMap.set(primeValue, groupedAnagrams.length);\n            groupedAnagrams.push([s]);\n        }\n    }\n    return groupedAnagrams;\n};\n```
['TypeScript']