You are given an array of the strings “words” of size 'M' and another string “result”. You have to treat it as an equation in which the left side is represented by the array “words” and the right side is represented by the string "result". Your task is to determine whether the equation is solvable or not under the following conditions:
1. Each character is decoded as a digit in the range [0, 9].
2. Each character must have only one mapping, and every pair of characters must map to different digits.
3. Each element of the array “words” and the string “result” are decoded as one number without the leading zeros.
4. The sum of the numbers on the left-hand side (words) must equal the number on the right-hand side (result).
Note:
1) The array “words”, and the string “result” contain only the uppercase English letters.
2) The number of different characters used in the expression is at most 10.
The first line contains an integer T, which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains a positive integer M denoting the size of the array “words”, as described in the problem statement.
The second line of each test case contains a sequence of M space-separated strings denoting the elements of the array.
The third line of each test case contains a string denoting the result string.
Output Format:
For each test case, print in a new line "true" if the equation is solvable and "false" otherwise.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
2 <= M <= 5
1 <= |WORDS[i]|, |RESULT| <= 7
The number of different characters used in the expression is at most 10.
Time Limit: 1sec
1
2
SEND MORE
MONEY
true
We can map ‘S’ -> 9, ‘E’ -> 5, ‘N’ -> 6, ‘D’ -> 7, ‘M’ -> 1, ‘O’ -> 0, ‘R’ -> 8, ‘Y’ -> 2. So, “SEND” will decode as 9567, “MORE” as 1085, and “MONEY” as 10652. Also the equation SEND + MORE = MONEY, 9567 + 1085 = 10652 will satisfy.
1
2
LEFT CODE
CODED
false
There is no mapping available that can satisfy the required equation.
Try to iterate over all the problems and get the possible outcome by dividing problem into sub problems. If further solution doesnot exists then traceback to initial one.
The idea here is to use the backtracking with one condition, which is explicitly mentioned in the problem itself that each word will decode as one number with no leading zeros. We know that if a + b = c, then a + b - c = 0 is also correct. So this is another condition which we are going to use in this approach.
First, add the ‘RESULT’ string in the array of string ‘WORDS’. We are traversing the array ‘WORDS’ from right to left column-wise. At each position, we have to know the ‘SUM’ we have so far, and for this, we'll use a variable named 'BALANCE', and we want it to be exactly zero at the end of all columns. So, if the 'BALANCE' is zero at the end of all columns, then return “true” as we have successfully found a valid mapping of the letter to the digit. If we completed a column and 'BALANCE' % 10 is not 0, then return “false”. Else we have to search in another column with balance as 'BALANCE' / 10. Because when we subtract the result string, we want each column’s last value to be zero, which we get by using the % function and the remaining part we’ll send for the further steps in the form of a carry, which is nothing but the updated 'BALANCE'.
If we are at the letter for which there is a prior mapping to digit, then we are going to use this mapping if the mapping is not zero. If it is zero, then the word has to be of length one, or the column in which we are currently at is not to be the last for that letter. Else, we have to assign a new mapping and call the function again.
Steps:
bool isAnyMapping(string words[], int row, int col, int bal, HashMap letToDig, char digToLet[], int totalRows, int totalCols):
O(N!), where N is the number of different characters used in the expression.
In the worst case, we have to check all possible combinations of the letter to the digit mapping, and at most, we can have N different letters. So, the time complexity will be O(N!).
O(N), where N is the number of different characters used in the expression.
As we are creating a HashMap for the letter to the digit mapping, which can store at most N characters in the worst condition and also recursion stack used for calling the function would be used for all the N characters. Hence, the overall space complexity will be O(N).