题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
import java.util.Scanner; public class Main { public static boolean fun(String str) { char[] ch = str.toCharArray(); int num = 0; boolean flag = false; for (int i = 0; i < ch.length; i++) { if (ch[i] == ch[ch.length - 1 - i]) { num++; } } if (num == str.length()) { flag = true; } return flag; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int f = 0; int max = 0; for (int i = 0; i < str.length(); i++) { for (int j = 2; j <= str.length() && i + j <= str.length() ; j++) { if (fun(str.substring(i, i + j))) { if (str.substring(i, i + j).length() > max) { max = str.substring(i, i + j).length(); } } } } System.out.println(max); } }