Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78fcd8c

Browse files
acbinyanglbme
authored andcommitted
Update and rename Soltuion.cpp to Solution.cpp
1 parent 3a22189 commit 78fcd8c

File tree

2 files changed

+103
-109
lines changed

2 files changed

+103
-109
lines changed

‎solution/1300-1399/1307.Verbal Arithmetic Puzzle/Soltuion.cpp‎

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
class Solution {
2+
public:
3+
bool isAnyMapping(vector<string>& words, int row, int col, int bal, unordered_map<char, int>& letToDig,
4+
vector<char>& digToLet, int totalRows, int totalCols) {
5+
// If traversed all columns.
6+
if (col == totalCols) {
7+
return bal == 0;
8+
}
9+
10+
// At the end of a particular column.
11+
if (row == totalRows) {
12+
return (bal % 10 == 0 && isAnyMapping(words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols));
13+
}
14+
15+
string w = words[row];
16+
17+
// If the current string 'W' has no character in the ('COL')th index.
18+
if (col >= w.length()) {
19+
return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols);
20+
}
21+
22+
// Take the current character in the variable letter.
23+
char letter = w[w.length() - 1 - col];
24+
25+
// Create a variable 'SIGN' to check whether we have to add it or subtract it.
26+
int sign;
27+
28+
if (row < totalRows - 1) {
29+
sign = 1;
30+
} else {
31+
sign = -1;
32+
}
33+
34+
/*
35+
If we have a prior valid mapping, then use that mapping.
36+
The second condition is for the leading zeros.
37+
*/
38+
if (letToDig.count(letter) && (letToDig[letter] != 0 || (letToDig[letter] == 0 && w.length() == 1) || col != w.length() - 1)) {
39+
40+
return isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
41+
letToDig, digToLet, totalRows, totalCols);
42+
43+
}
44+
// Choose a new mapping.
45+
else {
46+
for (int i = 0; i < 10; i++) {
47+
48+
// If 'i'th mapping is valid then select it.
49+
if (digToLet[i] == '-' && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) {
50+
digToLet[i] = letter;
51+
letToDig[letter] = i;
52+
53+
// Call the function again with the new mapping.
54+
bool x = isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
55+
letToDig, digToLet, totalRows, totalCols);
56+
57+
if (x == true) {
58+
return true;
59+
}
60+
61+
// Unselect the mapping.
62+
digToLet[i] = '-';
63+
if (letToDig.find(letter) != letToDig.end()) {
64+
letToDig.erase(letter);
65+
}
66+
}
67+
}
68+
}
69+
70+
// If nothing is correct then just return false.
71+
return false;
72+
}
73+
74+
bool isSolvable(vector<string>& words, string result) {
75+
// Add the string 'RESULT' in the vector 'WORDS'.
76+
words.push_back(result);
77+
78+
int totalRows;
79+
int totalCols;
80+
81+
// Initialize 'TOTALROWS' with the size of the vector.
82+
totalRows = words.size();
83+
84+
// Find the longest string in the vector and set 'TOTALCOLS' with the size of that string.
85+
totalCols = 0;
86+
87+
for (int i = 0; i < words.size(); i++) {
88+
89+
// If the current string is the longest then update 'TOTALCOLS' with its length.
90+
if (totalCols < words[i].size()) {
91+
totalCols = words[i].size();
92+
}
93+
}
94+
95+
// Create a HashMap for the letter to digit mapping.
96+
unordered_map<char, int> letToDig;
97+
98+
// Create a vector for the digit to letter mapping.
99+
vector<char> digToLet(10, '-');
100+
101+
return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);
102+
}
103+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /