Longest Palindromic Subsequence
LeetCode 516 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Interval DP: dp[i][j] = 2 + dp[i+1][j-1] if s[i] == s[j] else max(dp[i+1][j], dp[i][j-1]).
ನಾನು Longest Palindromic Subsequence (LeetCode 516) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — LCS ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Look at the two ends of a range. If they're equal, both can wrap the best palindrome inside: 2 + the inner range. If not, at least one end isn't used: drop the left or the right end and keep the better. A single character is a palindrome of length 1.
Approach
- 1
Base: every single character is length 1; empty ranges are 0.
- 2
Fill dp for start from right to left and end from start+1 to the right, so inner ranges are ready.
- 3
Apply the match / no-match rule.
- 4
dp[0][n-1] covers the whole string.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
lru_cache dfs(i, j) on the range.
dp[start][end] from shorter ranges.
Recursion + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
#case 1
#letters at the both the end are same
#case 2
#letters at both the end are not same
@lru_cache(maxsize=None)
def dfs(i,j):
if i==j:
return 1
if i>j:
return 0
if s[i]==s[j]:
return 2+dfs(i+1,j-1)
else:
return max(dfs(i+1,j),dfs(i,j-1))
return dfs(0,len(s)-1)Interval tabulationPythonO(n²) time · O(n²) space
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
#case 1
#letters at the both the end are same
#case 2
#letters at both the end are not same
n = len(s)
dp=[[0]*n for _ in range(n)]
for i in range(n):
dp[i][i]=1
for start in range(n-1,-1,-1):
for end in range(start+1,n):
if s[start]==s[end]:
dp[start][end]=2+dp[start+1][end-1]
else:
dp[start][end]=max(dp[start+1][end],dp[start][end-1])
return dp[0][n-1]Longest Palindromic Subsequence — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Longest Palindromic Subsequence ಹಿಂದಿನ intuition ಏನು?
Look at the two ends of a range. If they're equal, both can wrap the best palindrome inside: 2 + the inner range. If not, at least one end isn't used: drop the left or the right end and keep the better. A single character is a palindrome of length 1.
Longest Palindromic Subsequence (LeetCode 516) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Base: every single character is length 1; empty ranges are 0. 2. Fill dp for start from right to left and end from start+1 to the right, so inner ranges are ready. 3. Apply the match / no-match rule. 4. dp[0][n-1] covers the whole string.
Longest Palindromic Subsequence ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(n²). Space: O(n²). (Accepted approach: Interval tabulation.)
Longest Palindromic Subsequence ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Loop order matters — start must go downward so dp[start+1][…] is already filled. Equivalent shortcut: LCS of s and reversed s.
Longest Palindromic Subsequence ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Recursion + memo (O(n²) time · O(n²) space): lru_cache dfs(i, j) on the range. Interval tabulation (O(n²) time · O(n²) space): dp[start][end] from shorter ranges.