memo
with dimensions n x n
, where n
is the length of the string s
.helper(i, j)
that takes two pointers i
and j
as arguments, representing the current substring s[i...j]
.i >= j
, return 0, as a string with length 0 or 1 is already a palindrome.memo[i][j]
is not -1
, return its value as the result has already been computed.s[i]
equals s[j]
, set memo[i][j]
to the result of helper(i + 1, j - 1)
.s[i]
does not equal s[j]
, set memo[i][j]
to the minimum of helper(i + 1, j)
and helper(i, j - 1)
plus 1.memo[i][j]
as the result for the current subproblem.helper(0, n - 1)
and check if the result is less than or equal to k
. If it is, return true
; otherwise, return false
.