Java machine test question *: password interception (longest palindrome substring)

Time:2022-2-2

describe

Catcher is an intelligence agent of MCA country. During his work, he found that enemy countries will communicate with some symmetrical passwords, such as Abba, ABA, a, 123321, but they sometimes add some irrelevant characters at the beginning or end to prevent other countries from cracking. For example, make the following changes: Abba – > 12abba, ABA – > abakk, 123321 – > 51233214. Because the intercepted string is too long and there are many possible situations (abaaab can be regarded as the encryption form of ABA or baaab), cathcer’s workload is too large. He can only ask a computer expert for help. Can you help catcher find the longest valid password string?
 
 
Data range: string length meets

Enter Description:

Enter a string (the length of the string does not exceed 2500)

Output Description:

Returns the maximum length of a valid password string

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class Main {
    /**
     *Idea 1: find out all substrings and find the maximum effective substring length (it is effective if the string is the same after flipping and before flipping.) All local use cases can pass, but some in the machine test editor cannot pass. It is estimated that there is a bug in the question bank
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            List strs = new ArrayList();
            if(str.length() == 1) {
                strs.add(str);
            } else {
                for (int i = 0; i < str.length(); i++) {
                    for (int j = i+1; j <= str.length(); j++) {
                        strs.add(str.substring(i,j));
                    }
                }
            }
            int max = -1;
            StringBuffer sb;
            String temp;
            for (int i = 0; i < strs.size(); i++) {
                sb = new StringBuffer();
                sb.append(strs.get(i));
                temp = sb.reverse().toString();
                if(strs.get(i).equals(temp) && max < strs.get(i).length()) {
                    max = strs.get(i).length();
                }
            }
            System.out.println(max);
        }
    }

}

 

import java.util.Scanner;
public class Main {
    /**
     * 
     Idea 2: the center diffusion method of the longest palindrome substring, traversing each character as the middle bit for left-right comparison
     Algorithm flow:
     From right to left, each character is traversed, and each character needs to be processed twice, because there are two cases of palindrome substring:
     ABA type: it only needs to spread from the current character to both sides, compare whether the left and right characters are equal, and find out the length of the longest palindrome substring centered on the current character
     Abba type: it only needs to spread from the current character and the next character to both sides, compare whether the left and right characters are equal, and find out the length of the longest palindrome substring centered on the current character and the next character
     Finally, compare the lengths of the two types and take the longer length
    */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(solution(s));
    }
    
    private static int solution(String s) {
        int res = 0;
        for(int i = 0; i < s.length(); i++) {
            //ABA type
            int len1 = longest(s, i, i);
            //Abba type
            int len2 = longest(s, i, i + 1);
            res = Math.max(res, len1 > len2 ? len1 : len2);
        }
        return res;
    }
    
    private static int longest(String s, int l, int r) {
        while(l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            l--;
            r++;
        }
        return r - l - 1;
    }
}

Title Source:Niuke network

Reference link:https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1?tpId=37&&tqId=21255&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking