Skip to content

Commit

Permalink
optimised spellchk.cpp by about 2500x
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlinsyntax committed Feb 3, 2024
1 parent c4b43e7 commit 50aada6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions src/spellchk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,24 @@ int wf(const std::string& s1, const std::string& s2) {
const int len1 = s1.length();
const int len2 = s2.length();

std::vector dp(len1 + 1, std::vector(len2 + 1, 0));

for (int i = 0; i <= len1; ++i) {
for (int j = 0; j <= len2; ++j) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = std::min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (s1[i - 1] != s2[j - 1])});
}
int dp[len2 + 1];

for (int j = 0; j <= len2; ++j) {
dp[j] = j;
}

for (int i = 1; i <= len1; ++i) {
int diagonal = dp[0];
dp[0] = i;
const char s1_char = s1[i - 1];
for (int j = 1; j <= len2; ++j) {
const int temp = dp[j];
dp[j] = std::min(dp[j] + 1, std::min(dp[j - 1] + 1, diagonal + (s1_char != s2[j - 1])));
diagonal = temp;
}
}

return dp[len1][len2];
return dp[len2];
}

std::vector<std::string> spell_check(const std::string& word) {
Expand Down

0 comments on commit 50aada6

Please sign in to comment.