Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised Solution #3283

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Revised Solution #3283

wants to merge 13 commits into from

Conversation

CodersAcademy006
Copy link
Contributor

@CodersAcademy006 CodersAcademy006 commented Jul 18, 2024

class Solution {
public:
bool isAnyMapping(vector < string > & words, int row, int col, int bal, unordered_map < char, int > & letToDig,
vector < char > & digToLet, int totalRows, int totalCols) {
// If traversed all columns.
if (col == totalCols) {
return bal == 0;
}

// At the end of a particular column.
if (row == totalRows) {
    return (bal % 10 == 0 && 
        isAnyMapping(words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols));
}

string w = words[row];

// If the current string 'W' has no character in the ('COL')th index.
if (col >= w.length()) {
    return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols);
}

// Take the current character in the variable letter.
char letter = w[w.length() - 1 - col];

// Create a variable 'SIGN' to check whether we have to add it or subtract it.
int sign;

if (row < totalRows - 1) {
    sign = 1;
} else {
    sign = -1;
}

/*
    If we have a prior valid mapping, then use that mapping.
    The second condition is for the leading zeros.
*/
if (letToDig.count(letter) && 
    (letToDig[letter] != 0 || (letToDig[letter] == 0 && w.length() == 1) || col != w.length() - 1)) {
    
    return isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], 
        letToDig, digToLet, totalRows, totalCols);

}
// Choose a new mapping.
else {
    for (int i = 0; i < 10; i++) {

        // If 'i'th mapping is valid then select it.
        if (digToLet[i] == '-' && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) {
            digToLet[i] = letter;
            letToDig[letter] = i;
            
            // Call the function again with the new mapping.
            bool x = isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], 
                letToDig, digToLet, totalRows, totalCols);
            
            if (x == true) {
                return true;
            }

            // Unselect the mapping.
            digToLet[i] = '-';
            if (letToDig.find(letter) != letToDig.end()){
                letToDig.erase(letter);
            }
        }

    }

}

// If nothing is correct then just return false.
return false;

}

bool isSolvable(vector<string>& words, string result) {
    // Add the string 'RESULT' in the vector 'WORDS'.
words.push_back(result);

int totalRows;
int totalCols;

// Initialize 'TOTALROWS' with the size of the vector.
totalRows = words.size();

// Find the longest string in the vector and set 'TOTALCOLS' with the size of that string.
totalCols = 0;

for (int i = 0; i < words.size(); i++) {

    // If the current string is the longest then update 'TOTALCOLS' with its length.
    if (totalCols < words[i].size()) {
        totalCols = words[i].size();
    }

}

// Create a HashMap for the letter to digit mapping.
unordered_map < char, int > letToDig;

// Create a vector for the digit to letter mapping.
vector < char > digToLet(10, '-');

return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);

}

};

@idoocs idoocs added java Issues or Pull requests relate to .java code py Issues or Pull requests relate to .py code labels Jul 18, 2024
@idoocs
Copy link
Member

idoocs commented Jul 18, 2024

🤭 感谢你的提交,请检查你的改动是否符合以下项目规范。

1. 格式化

我们项目中各种编程语言代码(包括文档)所采用的格式化工具不同,提交 pr 之前必须确保代码、文档正确格式化。

  • .{md,js,ts,php,sql,rs} 采用 prettier
  • .{c,cpp,java} 采用 clang-format
  • .{py} 采用 black
  • .{go} 采用 gofmt
  • 其它待完善

2. Git 提交信息

我们项目遵循 AngularJS Git Commit Message Conventions 规范,我们希望你的提交信息尽可能与项目保持一致。

  • 新增或修改题解:feat: add/update solution(s) to lc problem(s): No.xxxx
  • 修复错误:fix: xxxx
  • 日常维护:chore: xxx

3. 其它补充

新增题解及代码时,需要创建 Solution.xxx 源代码文件(如果已存在,请确认算法是否更优,是则覆盖已有算法代码),同时,需要在 README.md 以及 README_EN.md 中添加对应的代码片段(英文文件中不要出现中文注释)
另外,编码风格(比如变量、函数的命名),尽量跟项目已有代码保持一致。


🤭 Thank you for your contribution. Please check if your changes comply with the following project specifications.

1. Formatting

We use different formatting tools for various programming languages (including documentation) in our project. You must ensure that the code and documentation are correctly formatted before submitting a pr.

  • .{md,js,ts,php,sql,rs} use prettier
  • .{c,cpp,java} use clang-format
  • .{py} use black
  • .{go} use gofmt
  • Others to be improved

2. Git Commit Message

Our project follows the AngularJS Git Commit Message Conventions. We hope that your submission information is as consistent as possible with the project.

  • Add or modify solutions: feat: add/update solution(s) to lc problem(s): No.xxxx
  • Fix errors: fix: xxxx
  • Routine maintenance: chore: xxx

3. Other notes

When adding solutions and code, you need to create a Solution.xxx source code file (if it already exists, please confirm whether the algorithm is better, if yes, overwrite the existing algorithm code), and at the same time, you need to add the corresponding code snippets in README.md and README_EN.md (do not have Chinese comments in the English file)
In addition, the coding style (such as the naming of variables and functions) should be as consistent as possible with the existing code in the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
java Issues or Pull requests relate to .java code py Issues or Pull requests relate to .py code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants