当前位置:网站首页>Returns the maximum number of palindromes in a string
Returns the maximum number of palindromes in a string
2022-08-04 02:34:00 【pshdhx_albert】
/*** @author pshdhx* @date 2022-08-03 15:20* @Des gives you a string s, find the longest palindrome substring in s.* input: s = "babad"* output: "bab"* Explanation: "aba" is also an answer that matches the meaning of the question.** input: s = "cbbd"* output: "bb"* @Method* If a string is a palindrome, then removing its first and last letters is still a palindrome;* @Summary*/
package com.pshdhx.Algorithm.middle;/*** @author pshdhx* @date 2022-08-03 15:20* @Des gives you a string s, find the longest palindrome substring in s.* input: s = "babad"* output: "bab"* Explanation: "aba" is also an answer that matches the meaning of the question.* * input: s = "cbbd"* output: "bb"* @Method* If a string is a palindrome, then removing its first and last letters is still a palindrome;* @Summary*/public class longest palindrome substring {public String longestPalindrome(String s) {int len = s.length();if (len < 2) {return s;}int maxLen = 1;int begin = 0;// dp[i][j] indicates whether s[i..j] is a palindromeboolean[][] dp = new boolean[len][len];// initialization: all substrings of length 1 are palindromesfor (int i = 0; i < len; i++) {dp[i][i] = true;}char[] charArray = s.toCharArray();// start recursionfor (int L = 2; L <= len; L++) {// length of palindromefor (int i = 0; i < len; i++) {//left boundary of palindrome// The right boundary can be determined by L and i, i.e. j - i + 1 = L we getint j = L + i - 1;//The right boundary of the palindrome// If the right boundary is out of bounds, you can exit the current loop [Note: the right boundary starts from 0, it cannot be greater than or equal to the length of the array]if (j >= len) {break;}if (charArray[i] != charArray[j]) {//If left boundary != right boundarydp[i][j] = false;} else {// left border == right borderif (j - i < 3) {//The length of the palindrome string is less than 4====" [1,2,3] If it is 2: left border = right border, true; if it is 3, then left border = right border, true, regardless of what is in the middle!dp[i][j] = true;} else {dp[i][j] = dp[i + 1][j - 1];}}// As long as dp[i][L] == true, it means that the substring s[i..L] is a palindrome, and record the length and starting position of the palindromeif (dp[i][j] && j - i + 1 > maxLen) {maxLen = j - i + 1;begin = i;}}}return s.substring(begin, begin + maxLen);}public static void main(String[] args) {String babad = new longest palindrome().longestPalindrome("babad");System.out.println(babad);}}
边栏推荐
猜你喜欢
随机推荐
[Original] Start the XPS/OXPS reader that comes with Windows 10
云开发旅游打卡广场微信小程序源码(含视频教程)
多线程间的通信方式你知道几种?
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
如何读取 resources 目录下的文件路径?
keytool命令
实例040:逆序列表
esp8266-01s刷固件步骤
Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu
Example 035: Setting the output color
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
SAP SD模块前台操作
【指针内功修炼】深度剖析指针笔试题(三)
QNX Hypervisor] 10.2 vdev 8259 2.2 user manual
STM8S105k4t6c--------------点亮LED
Utilities of Ruineng Micrometer Chip RN2026
查看mysql死锁语法
关联接口测试
pytorch应用于MNIST手写字体识别









