Longest parlidrome 516,1771, 2384

516. Longest Palindromic Subsequence
Medium

Given a string s, find the longest palindromic subsequence's length in s.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Example 1:

Input: s = "bbbab"
Output: 4
Explanation: One possible longest palindromic subsequence is "bbbb".

Example 2:

Input: s = "cbbd"
Output: 2
Explanation: One possible longest palindromic subsequence is "bb".

Constraints:

  • 1 <= s.length <= 1000
  • s consists only of lowercase English letters.

 

解法1: 这个题可以想象为 两个字符串,一个正向一个反向,求去 longest common subsequence

复制代码
class Solution {
    public int longestPalindromeSubseq(String s) {
        return lcs(s, 0, s.length()-1, new Integer[s.length()][s.length()]);
    }
    private int lcs(String s,int left,int right,Integer[][] mem){
        if(left>=s.length() || right<0) return 0;
        if(mem[left][right]!=null) return mem[left][right];
        if(s.charAt(left) == s.charAt(right)){
            mem[left][right]=lcs(s, left+1, right-1, mem)+1;
        }
        else{
             mem[left][right]= Math.max(lcs(s, left+1, right,mem), lcs(s,left, right-1,mem));
        }
        return mem[left][right];
    }
}
复制代码

解法2: 另外一个是dp 想法

这个视频讲的非常好https://www.youtube.com/watch?v=_nCsPn7_OgI

状态转移方程:

if arr[left] = arr[right]  : mem[left][right] = mem[left+1][right-1]+2;

else  mem[left][right] = max(mem[left][right-1], mem[left+1][right]);

复制代码
class Solution {
    public int longestPalindromeSubseq(String s) {
        int[][] mem = new int[s.length()][s.length()];
        for(int i=1;i<=s.length();i++){
            for(int j=0;j<=s.length()-i;j++){
                if(i == 1) {
                    mem[j][j+i-1] = 1;
                    continue;
                }
                if(s.charAt(j) == s.charAt(j+i-1)) 
                    mem[j][j+i-1] = mem[j+1][j+i-2]+2;
                else{
                    mem[j][j+i-1] = Math.max(mem[j+1][j+i-1], mem[j][j+i-2]);
                }
            }
        }
        return mem[0][s.length()-1];
    }
}
复制代码

 

1771. Maximize Palindrome Length From Subsequences
Hard

You are given two strings, word1 and word2. You want to construct a string in the following manner:

  • Choose some non-empty subsequence subsequence1 from word1.
  • Choose some non-empty subsequence subsequence2 from word2.
  • Concatenate the subsequences: subsequence1 + subsequence2, to make the string.

Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0.

A subsequence of a string s is a string that can be made by deleting some (possibly none) characters from s without changing the order of the remaining characters.

A palindrome is a string that reads the same forward as well as backward.

Example 1:

Input: word1 = "cacb", word2 = "cbba"
Output: 5
Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.

Example 2:

Input: word1 = "ab", word2 = "ab"
Output: 3
Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.

Example 3:

Input: word1 = "aa", word2 = "bb"
Output: 0
Explanation: You cannot construct a palindrome from the described method, so return 0.

 Constraints:

  • 1 <= word1.length, word2.length <= 1000
  • word1 and word2 consist of lowercase English letters.
复制代码
class Solution {
    public int longestPalindrome(String word1, String word2) {
        int len1 = word1.length(), len2 = word2.length();
        int[][] mem = new int[len1+len2][len1 + len2];
        String combine = word1 + word2;
        dp(combine, mem);
        int left = 0, right = len1 + len2 - 1;
        int result = 0;
        for(int i=0;i<len1;i++){
            for(int j=0;j<len2;j++){
                if(word1.charAt(i) == word2.charAt(j)){
                    result = Math.max(result, mem[i+1][j+len1-1]+2);
                }
            }
        }
        return result;
    }
    private void dp(String word,int[][] mem){
        int len = word.length();
        for(int i=1;i<=len;i++){
            for(int j=0;j<=len-i-1;j++){
                if(i == 1){
                    mem[j][j+i-1] = 1;
                    continue;
                }
                if(word.charAt(j) == word.charAt(j+i-1)) 
                    mem[j][j+i-1] = mem[j+1][j+i-2] + 2;
                else
                    mem[j][j+i-1] = Math.max(mem[j][j+i-2], mem[j+1][j+i-1]);
            }
        }
    }
}
复制代码

 

You are given a string num consisting of digits only.

Return the largest palindromic integer (in the form of a string) that can be formed using digits taken from num. It should not contain leading zeroes.

Notes:

  • You do not need to use all the digits of num, but you must use at least one digit.
  • The digits can be reordered.

 

Example 1:

Input: num = "444947137"
Output: "7449447"
Explanation: 
Use the digits "4449477" from "444947137" to form the palindromic integer "7449447".
It can be shown that "7449447" is the largest palindromic integer that can be formed.

Example 2:

Input: num = "00009"
Output: "9"
Explanation: 
It can be shown that "9" is the largest palindromic integer that can be formed.
Note that the integer returned should not contain leading zeroes.

Constraints:

  • 1 <= num.length <= 105
  • num consists of digits.
复制代码
class Solution {
    /**
    坑点:
    1. 0开头的话,要把0去掉,比如 00009 -> 不是00900 而是9
    2. 空的话,要返回0, 比如:""
     */
    public String largestPalindromic(String num) {
        int[] count = new int[10];
        for(char c : num.toCharArray()) {
            count[c - '0']++;
        }
        StringBuffer sb = new StringBuffer();
        int max = -1;
        for(int i = 9; i >= 0; i--) {
            for(int j = 0; j < count[i]/2; j++) {
                sb.append((char)('0' + i));
            }
            count[i] = count[i] % 2;
            if(count[i] == 1 && max == -1) max = i; 
        }
        if(sb.length() > 0 && sb.charAt(0) == '0') sb = new StringBuffer();
        String result = sb.toString() + (max == -1 ? "" : (char)('0' + max)) + sb.reverse().toString();
        return result.length() == 0 ? "0" : result.toString();
    }
}
复制代码

 

posted @   xiaoyongyong  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示