Longest Palindromic Subsequence
https://leetcode.com/problems/longest-palindromic-subsequence/description/
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Thoughts
数组找长度想DP。刚开始还想是不是用背包做,发现做不出来。实际上沿着回文一般解法即可,让f[j][i]表示从j到i最长回文长度,对每个j, i判断是否相等,相等意味着保留ij能构成新的回文,旧长度+2。不等则意味着j和i不能共存, 可以是从j到i-1最长回文长度或者j + 1到i的。
Code
Analysis
时间复杂度为O(n^2).
Previous5. Longest Palindromic SubstringNext1312. Minimum Insertion Steps to Make a String Palindrome
Last updated
Was this helpful?