Address https://leetcode-cn.com/problems/verbal-arithmetic-puzzle/
Title Description
give you an equation on the left by words expressed, represented by the right result.
Whether you need to check the following rules solvable equation:
Each character will be decoded into a digit (0--9).
Each pair of different characters must be mapped to different numbers.
Each words [i] and the result will not be decoded into a number of leading zeros.
Left digital sum (words) is equal to the right number (result).
If the equation is solvable, return True, otherwise False.
Example 1 : Input: words = [ " the SEND " , " the MORE " ], Result = " MONEY " Output: to true explained: mapping ' S ' -> . 9 , ' E ' -> . 5 , ' N ' -> . 6 , ' D ' - > . 7 , ' M ' -> . 1 , ' O ' -> 0 , 'R'->8, 'Y'->'2' 所以 "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 示例 2: Input: words = [ " the SIX " , " SEVEN " , " SEVEN " ], Result = " TWENTY " Output: to true explained: mapping ' S ' -> . 6 , ' the I ' -> . 5 , ' X- ' -> 0 , ' E ' -> . 8 , ' V ' -> . 7 , ' N ' ->2, 'T'->1, 'W'->'3', 'Y'->4 所以 "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 示例 3: 输入:words = ["THIS","IS","TOO"], result = "FUNNY" 输出:true 示例 4: Input: words = [ " LEET " , " CODE " ], Result = " the POINT " Output: to false prompt: 2 <= words.length <= 5 1 <= words[i].length, results.length <= 7 words [i], result contains only capital letters The number of different characters used in the expression for the maximum 10
Algorithm 1
herein with reference to the idea of big brother and other own work.
The effect is still not ideal can only say that Zi is correct final realization bumps efficiency is still relatively low. It can be considered ac
DFS violent force following ideas each letter must be replaced numbers of TLE
after then each of the digital Alternatively each column, the column is calculated by the following letters whether the sum of all digital conflict result corresponding number can be reduced without the necessary calculations.
Using two variables map <char, int> letterMapNum; map <int, int> usedNum;
recording the numbers and letters replace those numbers are used
further to calculate the maximum length of the string in order to determine the boundary need to loop (all in accordance with the maximum length of the cycle, then also to identify whether the shorter string cross-border portion)
The rest is replaced by DFS traversal and letter substitutions CHECK last digit is correct
class Solution { public: map<char, int> letterMapNum; map<int, int> usedNum; int maxStringLen = 0; int GetmaxStringLen(const vector<string>& words) { int ret = 0; for (auto& e : words) {ret = max((int)e.size(), ret);} return ret; } bool Check(const vector<string>& words, const string& result) { int sum = 0; int tn = 0; for (int i = 0; i < words.size(); i++) { string s = words[i]; reverse(s.begin(), s.end()); tn = 0; for (int j = 0; j < s.size(); j++) { int num = letterMapNum[s[j]]; Tn = Tn * 10 + the num; } sum += tn; } string s; while (sum != 0) { int e = sum % 10; sum = sum / 10; if (usedNum[e] == 0) {s += "?";} else { for (auto& ele : letterMapNum) { if (ele.second == e) { s += ele.first; break; } } } } if (s.size() != result.size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] == '?' ) { char c = result[i]; if (letterMapNum[c] != -1)return false; } else if (s[i] != result[i]) {return false;} } return true;; } bool Dfs(int x,int y , int sum ,const vector<string>& words,const string& result) { bool K = false ; if (x == words.size() && y == maxStringLen) { //ret = check ret = Check(words, result); return ret; } IF (X> = words.size ()) { // detected on one and the compliance requirements int resultC = Result [Y]; IF ! (letterMapNum [resultC] = - . 1 ! && letterMapNum [resultC] = SUM% 10 ) return to false ; // the column and carry into the next column SUM = SUM / 10 ; // computing the next column and all the letters X = 0 ; Y ++ ; } IF (Y < words [X] .size ()) { char C = words [X] [Y]; IF (! letterMapNum [C] = - . 1 ) { // have the letter substitutions SUM + = letterMapNum [C ]; IF (Y == words [X] .size () - . 1 && letterMapNum [C] == 0 ) { return to false ;} IF (the Dfs (X + . 1 , Y, SUM, words, Result)) return to true ; } the else { // does not replace the letter int Start = 0 ; IF (Y == words [X] .size () - . 1 ) Start = . 1 ; for ( int I = Start; I <= . 9 ; I ++ ) { IF (usedNum [I] =! 0 ) Continue ; int oldsum = sum; int oldmap = letterMapNum[c];int oldUsed = usedNum[i]; usedNum[i] = 1; letterMapNum[c] = i; sum += i; if (Dfs(x + 1, y, sum, words, result)) return true; sum = oldsum; letterMapNum[c] = oldmap; usedNum[i] = oldUsed; } } } else { if (Dfs(x + 1, y, sum, words, result)) return true; } return false; } bool isSolvable(vector<string>& words, string result) { bool ret = false; for (int i = 0; i < words.size(); i++) reverse(words[i].begin(), words[i].end()); reverse(result.begin(), result.end()); maxStringLen = GetmaxStringLen(words); if (result.size() > (maxStringLen + 1)) return ret; for (int i = 0; i < 26; i++) {char a = 'A';a += i;letterMapNum[a] = -1;} right = Dfs ( 0 , 0 , 0 , ord, result); Return the right; } };