Hi,
I have problems solving that, I’m able to use Levensthein distance algorithm to show what is the length of sequence but I’m unable to show the array.
int getLevDistDP(std::string const& curr, int currIdx, std::string const& orig, int origIdx, std::vector<std::vector<int>>& cache) {
if (currIdx < 0) {
return 0;
} else if (origIdx < 0) {
return 0;
}
if (cache[currIdx][origIdx] == -1) {
if (curr[currIdx] == orig[origIdx]) {
cache[currIdx][origIdx] = 1 + getLevDistDP(curr, currIdx - 1, orig, origIdx - 1, cache);
} else {
int addToTheEnd = getLevDistDP(curr, currIdx, orig, origIdx - 1, cache);
int removeFromEnd = getLevDistDP(curr, currIdx - 1, orig, origIdx, cache);
int substituteLast = getLevDistDP(curr, currIdx - 1, orig, origIdx - 1, cache);
cache[currIdx][origIdx] = std::max({addToTheEnd, removeFromEnd, substituteLast});
}
}
return cache[currIdx][origIdx];
}
int getLevensteinDistance(std::string const& current, std::string const& original) {
std::vector<std::vector<int>> cache(original.size(), std::vector<int>(current.size(), -1));
return getLevDistDP(current, current.size() - 1, original, original.size() - 1, cache);
}
Basically I don’t know how to convey the vector or string of characters which would be the result. Any help appreciated.
Thanks,
It looks good to me. If it passes your unit tests, then you’re finished. Well done!