Hi,
There is couple of explanations for similar problems on the web which I don’t buy. I would like to know is this is the correct way of doing that.
My initial cache looks like this:
0, 1, 2, 3, 4,<- those values denotes the number of characters needed to deleted to make it empty string, the same 1 col
1, 0, 0, 0, 0,
2, 0, 0, 0, 0,
3, 0, 0, 0, 0,
4, 0, 0, 0, 0,
I’m trying to solve this using Lev distance modified algorithm, in which I don’t care about modifying characters i.e:
void getLevenstein(std::string const& current, std::string const& currReversed, std::vector<std::vector<int>>& cache) {
for (int i = 1; i <= current.size(); ++i) {
for (int j = 1; j <= current.size(); ++j) {
if (current[i-1] == currReversed[j-1]) {
cache[i][j] = cache[i-1][j-1];
} else {
cache[i][j] = 1 + std::min({cache[i-1][j], cache[i][j-1]});
}
}
}
}
My idea is simple, I count number of deletes for string to make it reverse of itself. So basically i try to find a min pivot in cache array after computation that is minimum, by going diagonal. For input with even number of characters it should be in the diagonal path as number of characters has to be the same from front and back of the string in palindrome, for example for word “even” resulting cache looks like this:
0, 1, 2, 3, 4,
1, 2, 1, 2, 3,
2, 3, 2, 1, 2,
3, 4, 3, 2, 1,
4, 3, 4, 3, 2,
Minimum is 2.
But for word with odd number of characters, diagonal path is not enough we should also check upper and lower entries, as palindrome cannot be perfectly sliced to half.
So for word “odd” resulting cache looks like this:
0, 1, 2, 3,
1, 2, 3, 2,
2, 1, 2, 3,
3, 2, 1, 2,
And min is 1
Is that correct way of doing that or I should take some other approach?
Thanks,