A cryptarithm is a mathematical puzzle for which the goal is to find the correspondence between letters and digits, such that the given arithmetic equation consisting of letters holds true when the letters are converted to digits.
You have an array of strings
crypt, the cryptarithm, and an an array containing the mapping of letters and digits,
solution. The array
crypt will contain three non-empty strings that follow the structure:
[word1, word2, word3], which should be interpreted as the
word1 + word2 = word3 cryptarithm.
crypt, when it is decoded by replacing all of the letters in the cryptarithm with digits using the mapping in
solution, becomes a valid arithmetic equation containing no numbers with leading zeroes, the answer is
true. If it does not become a valid arithmetic solution, the answer is
crypt = ["SEND", "MORE", "MONEY"] and
solution = [['O', '0'],
the output should be
isCryptSolution(crypt, solution) = true
When you decrypt
"MONEY" using the mapping given in
crypt, you get
9567 + 1085 = 10652 which is correct and a valid arithmetic equation.
crypt = ["TEN", "TWO", "ONE"] and
solution = [['O', '1'],
the output should be
isCryptSolution(crypt, solution) = false
054 + 091 = 145,
091 both contain leading zeroes, meaning that this is not a valid solution.
- [input] array.string crypt
An array of three non-empty strings containing only uppercase English letters.
crypt.length = 3,
1 ≤ crypt[i].length ≤ 14.
- [input] array.array.char solution
An array consisting of pairs of characters that represent the correspondence between letters and numbers in the cryptarithm. The first character in the pair is an uppercase English letter, and the second one is a digit in the range from
solution[i].length = 2,
'A' ≤ solution[i] ≤ 'Z',
'0' ≤ solution[i] ≤ '9',
solution[i] ≠ solution[j], i ≠ j,
solution[i] ≠ solution[j], i ≠ j.
It is guaranteed that
solution only contains entries for the letters present in
crypt and that different letters have different values.
- [output] boolean
true if the
solution represents the correct solution to the cryptarithm
crypt, otherwise return
Map the letters to the given numbers.
Generate corresponding numbers,
if first + second = third, check if they have leading zeros by comparing the length of numbers and the words, if not, return 1
f they have leading zeros or the sum is not equal, return 0