Edit String to Palindrome

Given a string, find the minimum number of characters to edited to convert it to palindrome.


Recursive

public int edit(char str[], int l, int h)
{
    if (l > h) return Integer.MAX_VALUE;
    if (l == h) return 0;
    if (l == h - 1) return (str[l] == str[h])? 0 : 1;

    return (str[l] == str[h])? edit(str, l + 1, h - 1):
                                Math.min(edit(str, l + 1, h - 1) + 1,
                               (Math.min(edit(str, l, h - 1),
                                   edit(str, l + 1, h)) + 1));
}

results matching ""

    No results matching ""