educative.io

Educative

Longest Palindromic Substring for loops

I am not able to understand below for loops iterations around startIndex and endIndex. Things like why first loops starts with startIndex and so on.

for (int startIndex = st.length() - 1; startIndex >= 0; startIndex–) {
for (int endIndex = startIndex + 1; endIndex < st.length(); endIndex++) {
if (st.charAt(startIndex) == st.charAt(endIndex)) {
// if it’s a two character string or if the remaining string is a palindrome too
if (endIndex - startIndex == 1 || dp[startIndex + 1][endIndex - 1]) {
dp[startIndex][endIndex] = true;
maxLength = Math.max(maxLength, endIndex - startIndex + 1);
}
}
}
}