当前位置:网站首页>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);}}
边栏推荐
- Use of lombok annotation @RequiredArgsConstructor
- lombok注解@RequiredArgsConstructor的使用
- 返回字符串中的最大回文数
- 单片机C语言->的用法,和意思
- cdh6.x 集成spark-sql
- ant-design的Select组件采用自定义后缀图标(suffixIcon属性)时,点击该自定义图标没有反应,不会展示下拉菜单的问题
- 融云「音视频架构实践」技术专场【内含完整PPT】
- Flask Framework Beginner-05-Command Management Manager and Database Use
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- 小程序:扫码打开参数解析
猜你喜欢

持续投入商品研发,叮咚买菜赢在了供应链投入上

html select tag assignment database query result

Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon

lombok注解@RequiredArgsConstructor的使用

Priority_queue element as a pointer, the overloaded operators

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

Example 041: Methods and variables of a class

Parquet encoding

自制蓝牙手机app控制stm8/stm32/C51板载LED

Instance, 038: the sum of the diagonal matrix
随机推荐
Small Turtle Compilation Notes
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
云开发旅游打卡广场微信小程序源码(含视频教程)
keytool命令
瑞能微计量芯片RN2026的实用程序
第13章 网络安全漏洞防护技术原理与应用
实例037:排序
Instance, 038: the sum of the diagonal matrix
DHCP服务详解
Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
C program compilation and predefined detailed explanation
[Original] Start the XPS/OXPS reader that comes with Windows 10
cdh6.x 集成spark-sql
idea中diagram使用
2022.8.3-----leetcode.899
什么是SVN(Subversion)?
Example: 036 is a prime number
lombok注解@RequiredArgsConstructor的使用