Skip to content

Instantly share code, notes, and snippets.

@nthomson
Last active August 29, 2015 14:18
Show Gist options
  • Save nthomson/7b34727562745b192cd2 to your computer and use it in GitHub Desktop.
Save nthomson/7b34727562745b192cd2 to your computer and use it in GitHub Desktop.
Cryptographic Equivalence

Cryptographic Equivalence:

A word is considered to be cryptographically equivalent to another word if one word could conceivably be a ciphered using a simple substitution cipher (a 1 to 1 mapping of characters) into the other word.

For example, the words "mom" and "dad" are cryptographically equivalent.

With a cipher that looks like this:

...
m => d
o => a
...

"mom" would be ciphered to "dad"

However, "boo" and "mom" are not cryptographically equivalent. Because there is no simple substituion cipher that will allow you to cipher "boo" as "mom"

Given an array of words, write a function that will print on each line all the words that are cryptographically equivalent to each other.

Given the array ['mom', 'dad', 'brother', 'pa', 'ma', 'boo', 'food', 'book'] The function would print, in no particular order.

mom, dad
brother
pa, ma
food, book
@TomaQ
Copy link

TomaQ commented Apr 3, 2015

One way that I just thought of would be to iterate through the array and assign each word a sequence of numbers based on the uniqueness of their characters. So to start off, it would look at 'mom' and see that there is an 'm' and assign it to 1, then it would see 'o', assign it to 2, and then see there is another 'm' and assign it 1. So 'mom' would be 121, 'food' would be 1221 etc. You could have a temporary array of characters (for each word) that the function looks through to see if it has already assigned a number to a character. It would then add it to the array and assign it index+1 (just for easier readability). So 'mom' would have an array with {'m', 'o'}, brother would have {'b', 'r', 'o', 't', 'h', 'e'} etc. Then it just compares the array of 'wordValues', which is the numbers assigned to the words, and prints out the ones that match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment