516. Longest Palindromic Subsequence

2023-05-16

516. Longest Palindromic Subsequence

Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.

Example 1:
Input:

"bbbab"

Output:

4

One possible longest palindromic subsequence is “bbbb”.

注意这里是subsequence,而不是substring

Example 2:
Input:

"cbbd"

Output:

2

One possible longest palindromic subsequence is “bb”.

solution1

首先我们要了解什么是最长回文子序列

经典算法——最长回文子序列中这样解释道:

一个字符串有许多子序列,比如字符串cabbeaf,它的子序列有c、abb、e、a、f,可以通过删除某些字符而变成回文字符串,字符串“cabbeaf”,删掉‘c’、‘e’、‘f’后剩下的子串“abba”就是回文字符串,也是其中最长的回文子序列。注意和最长回文子串区别,最长回文子串必须是连续的,这里的最长回文子序列,可以是不连续的,这就是最长回文子序列LPS问题。

下面的视频来自于

Longest Palindromic Subsequence | Dynamic Programming | Set 12 | GeeksforGeeks
在这里插入图片描述

在这里插入图片描述

class Solution {
    public int longestPalindromeSubseq(String s) {
        if(s == null || s.length() == 0)
            return 0;
        int len = s.length();
        int[][] max = new int[len][len];
        
        
        for(int i = 0; i < len; i++){
            max[i][i] = 1;
        }
        
        for(int l = 1; l <= len; l++){
            for(int start = 0; start < len - l; start++){
                int end = start + l;
                if(s.charAt(start) == s.charAt(end)){
                    max[start][end] = max[start+1][end-1] + 2;
                }else{
                    max[start][end] = Math.max(max[start+1][end],max[start][end-1]);
                }
            }
        }
        return max[0][len-1];
        
    }
}

solution2_not ac

class Solution {
   int left = 0;
    int right = 0;

    int maxlen = 0;
    public int longestPalindromeSubseq(String s) {
        int longestLen = 1;

        if(s == null || s.length() == 0) return 0;
        if(s.length() == 1) return 1;

        int len = s.length();


        for(int j = 1; j <= len - 1; j++){
            for(int i = 0; i < j; i++){

                maxlen = 0;
                String s1 = s.substring(i, j+1);
                if(isPalindrome(s1,0,s1.length()-1))
                {
                    maxlen += right-left +1;
                    longestLen = Math.max(maxlen,longestLen);

                }
            }
        }


        return longestLen;
    }


    public boolean isPalindrome(String s,int start,int end){

        if(s.length() == 1 ) {
            left = start;
            right = end;
            return true;
        }

        if(s.charAt(start) == s.charAt(end) ){
            if(end - start <= 2){
                left = start;
                right = end;
                return true;

            }else {
                maxlen += 2;
                String substring = s.substring(start + 1, end);
                return  isPalindrome(substring,0,substring.length()-1);
            }


        }else {
            if(end - start == 1)
                return false;
            else if(s.charAt(start) == s.charAt(end-1) || s.charAt(start+1) == s.charAt(end)){
                if(s.charAt(start) == s.charAt(end-1) ){
                    String s3 = s.substring(start, end );
                    return isPalindrome(s3,0,s3.length()-1);
                }

                if(s.charAt(start+1) == s.charAt(end)){
                    String s2 = s.substring(start + 1, end + 1);
                    return isPalindrome(s2,0,s2.length()-1);

                }
            }
        }

        return false;

    }


}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

516. Longest Palindromic Subsequence 的相关文章

随机推荐

  • 【网络教程】群晖自动更新 Docker 映像与容器(Watchtower)【转】

    群晖自动更新 Docker 映像与容器 xff08 Watchtower xff09 更多内容
  • git多账号配置

    什么叫多账号配置 xff0c 也就是说假如你在公司用的gitlab服务器 xff0c 但是自己还有用到GitHub xff0c 那么此时你在本地就需要配置多个ssh key 步骤如下 xff1a 利用ssh keygen t rsa f g
  • Ubuntu 18.04和windows建立共享文件夹

    1 安装samba sudo apt install samba sudo apt install cifs utils 2 创建共享目录 mkdir home yourname share yourname是home下一个文件夹 xff0
  • Linux权限详解,多用户多组各种权限配置原理

    网上有太多关于Linux权限详解 xff0c 这里不啰嗦那些 主要解释下这些权限是杂用的 xff0c 否则知道了什么用户 组之类的权限也配不好 一 权限分类 r xff1a 读取权限 xff0c 数字代号为 34 4 34 w xff1a
  • 1.tow sum

    文章目录 题目c 43 43 版本java版本利用hashmap正确做法 题目 Two Sum Easy Given an array of integers return indices of the two numbers such t
  • 2. Add Two Numbers

    2 Add Two Numbers Medium 59971568Share You are given two non empty linked lists representing two non negative integers T
  • Linux VNC server的安装及简单配置使用

    Linux VNC server的安装及简单配置使用 转 xff1a http www 2cto com os 201309 241104 html Linux VNC server的安装及简单配置使用 摘要 xff1a Linux vnc
  • 3. Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Given a string find the length of the longest substring without repeating
  • 4. Median of Two Sorted Arrays

    Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n respectively Find the media
  • 7. Reverse Integer

    文章目录 Reverse IntegersolutionAlgorithm Reverse Integer Easy Given a 32 bit signed integer reverse digits of an integer Ex
  • 8. String to Integer (atoi)

    String to Integer atoi Implement atoi which converts a string to an integer The function first discards as many whitespa
  • 9. Palindrome Number

    Palindrome Number Easy Determine whether an integer is a palindrome An integer is a palindrome when it reads the same ba
  • 1071. Greatest Common Divisor of Strings

    1071 Greatest Common Divisor of Strings Easy 30985Share For strings S and T we say 34 T divides S 34 if and only if S 61
  • 这个问题搞了我一天

  • 150逆波兰式

    文章目录 150 Evaluate Reverse Polish NotationSolution 150 Evaluate Reverse Polish Notation Medium Evaluate the value of an a
  • 收到礼物最大值

    题目描述 在一个m n的棋盘的每一个格都放有一个礼物 xff0c 每个礼物都有一定价值 xff08 大于0 xff09 从左上角开始拿礼物 xff0c 每次向右或向下移动一格 xff0c 直到右下角结束 给定一个棋盘 xff0c 求拿到礼物
  • 64. Minimum Path Sum

    64 Minimum Path Sum Given a m x n grid filled with non negative numbers find a path from top left to bottom right which
  • 找出亲密对数

    题目内容 xff1a 求数n之内的亲密对数 所谓 亲密对数 xff0c 即A的所有因子 xff08 包含1但不包含其本身 xff09 之和等于B xff0c 而B的所有因子之和等于A 输入格式 某个数字n 输出格式 xff1a 此数字n之内
  • 5. Longest Palindromic Substring

    5 Longest Palindromic Substring Given a string s find the longest palindromic substring in s You may assume that the max
  • 516. Longest Palindromic Subsequence

    516 Longest Palindromic Subsequence Given a string s find the longest palindromic subsequence s length in s You may assu