

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).
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.
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.
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
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.
bool isAnyMapping(string words[], int row, int col, int bal, HashMap letToDig, char digToLet[], int totalRows, int totalCols):