5 ms·
on a tangent, one way could be assign a number code to each alphabet. Add the numbers that occur in the strings. IF the sum matches, they are anagrams.
by mindhash 8y ago
on a tangent, one way could be assign a number code to each alphabet. Add the numbers that occur in the strings. IF the sum matches, they are anagrams.
- aquateen 8y agoI don't see how the sum would be unique to a particular combination of letters.
- LyndsySimon 8y ago“ad” = “bc” You’re right.
- jcadam 8y agoI briefly mused about just summing the ASCII codes for each letter in the strings. But quickly discarded the idea for this reason :)
- tylerhou 8y agoWorks if you assign prime numbers to each letter and multiply instead. So a=2, b=3, etc.
- tuukkah 8y agoTo get decent anagram lengths and complexity, implement the numbers as a dict of repetitions of primes, and implement the multiplication by summing the repetitions. ;-)
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]
- D_Alex 8y agoWhat if a=1, b=10, c=100 - etc? Assuming the strings were English words...
- jgforbes 8y agoa=1, b=2, c=3, ... ac = 1 + 3 = 4 bb = 2 + 2 = 4
- nomel 8y agoYou would have to make sure the sum of any combination of all characters was unique. For example, if the code was the character number, a=1, b=2, etc, both "abc" and "bbb" would have the same sum. So I think something silly like: character_code = len(string)*len(alphabet)^character_index should work.
- eecc 8y agoI guess the numbers should be primes... 4+1=3+2
- shagie 8y ago10 = 5 + 5 = 7 + 3
- eecc 8y agothat's right... even prime isn't enough
- shagie 8y agoPrime is ok if it’s multiplied (there is one and only one prime factorization of a number).... but that can get to absurdly large numbers. Still, consider the word ‘abe’ with a = 2, b = 3, c = 5, d = 7 and e = 11. abe would then be 2^1 * 3^1 * 5^0 * 7^0 * 11^1. ‘abba’ would be 2^2 * 3^2. Each anagram would have a distinct value. See also Gödel numbering and FRACTRAN.
- eecc 8y agoThat’s right... stupid me, it’s the base of public key cryptography (doh)
- pg_bot 8y agoYou need to multiply the numbers, (and they must be prime) not add them.
- deleted 8y ago[deleted]