n
to the length of s
.memo
with dimensions n x n
, initialized with zeros.lps
that takes parameters i
, j
, and memo
.
memo[i][j]
is not zero, return its value (memoization).i > j
, return 0 (base case: empty substring).i == j
, return 1 (base case: single character substring).s[i] == s[j]
, set memo[i][j]
to 2 + lps(i + 1, j - 1, memo)
.memo[i][j]
to max(lps(i, j - 1, memo), lps(i + 1, j, memo))
.lps(0, n - 1, memo)
.