Archive for August, 2021

Create 2D palindrome table

August 11, 2021

Below, we will see code to create a 2D palindrome table, that can be used to query if a string from index i to j is palindrome or not. The code is very simple and intuitive. We run a loop from end of the string, in another loop that starts forward from that index (i + 1), we look till the end if characters at index i and j match. If they do, then we set dp[i][j] to true if i + 1 is equal to j or dp[i+1][j-1] was true. Before coming to index dp[i][j], we would have already visited dp[i+1][j-1], therefore it let’s you set the table using previous values that had been already checked.

(more…)