Given two strings, find the translation table (substitution cipher) between the two, if the translation is not possible, output false. The answer must be minimized and created from left-to-right. The first character to be translated between words must be the first in the translation table. In addition to this, any letter that is not translated (in the same place as it was originally), should NOT be in the translation table.
Probably most easily defined through examples:
"bat", "sap" => ["bt","sp"]
Notice the ordering, an output of
["tb","ps"] is not valid for this challenge.
"sense", "12n12" => ["se","12"]
Notice how the
n isn't translated because it is a 1 to 1 relation.
"rabid", "snail" => ["rabd","snal"]
Notice how the
i isn't translated because it is a 1 to 1 relation.
"ass", "all" => ["s","l"]
A is not included, it stays the same,
s can map to
l due to pattern match.
"3121212", "ABLBLBL" => ["312","ABL"]
Matches pattern perfectly.
"banana", "angular" => false
(not the same length, impossible).
"animal", "snails" => false
(each character can only be used ONCE on each side of the translation).
"can","cnn" => false
(n is implicitly used in translation, therefore, defining a translation table with n->a would be invalid)
[aimal,sails] is an invalid answer, making this falsy.
"a1", "22" => false
See "caveats", this is listed as falsy. In this case, it's because
1 cannot both map to
2. (Each character can only be used ONCE on each side of the translation).
This answer seems to be a good benchmark: https://codegolf.stackexchange.com/a/116807/59376
If you have questions about the functionality of two unlisted word pairs, defer to this implementation.
- Input may be as a 2 element array or as 2 separate inputs.
- Output can be as an array or newline/space delimited, similar to how I have it shown.
- False output may be 0, -1 or false. Erroring/Empty output is also fine.
- You are guaranteed that
awill not equal
bwill be empty.
bare printable-ASCII-only sequences of letters.
- Translations must occur from left to right, see example 1.
- You must not output characters that remain the same.
- Your program may only take in two strings
- Each character can only be used ONCE on each side of the translation. This is what makes the translation from
- Recursive replaces should not occur. Example of recursive replace:
"a1","22"->[a1,12]where a is first replaced by a 1, then both resultant 1's are replaced with 2's. This is not correct, assume all translations occur independent of each other, meaning this is falsy. Meaning: "a1" with translation table of [a1,12] evaluates to 12 (not 22)