Grokking Dynamic Programming Patterns for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
I find my solution more elegant

Mohammed Dh Abbas

Oct 23, 2024

class Solution: def findLPSLength(self, st): def is_palindrom(s): i = 0 j = len(s) - 1 while i <= j: if s[i] != s[j]: return False i += 1 j -= 1 return True def dp(index, s, memo): if s in memo: return memo[s] if is_palindrom(s): return len(s) if index >= len(s): return 0 skip_char = dp(index + 1, s[:], memo) with_char = dp(index + 1, s[:index] + s[index+1:], memo) result = max(skip_char, with_char) memo[s] = result return result return dp(0, st, {})

0

0

Comments
Comments

On this page

Problem Statement

Try it yourself