当前位置:网站首页>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);}}
边栏推荐
- 倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
- P3384 【模板】轻重链剖分/树链剖分
- FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
- Flink原理流程图简单记录
- DDTL: Domain Transfer Learning at a Distance
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- activiti流程执行过程中,数据库表的使用关系
- What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
- Zabbix设置邮件告警+企业微信告警
- Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
猜你喜欢
DHCP服务详解
idea中diagram使用
In a more general sense, calculating the displacement distance and assumptions
Pine脚本 | 如何显示和排版绘图开关?
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
小程序:扫码打开参数解析
MallBook 助力SKT思珂特教育集团,立足变化,拥抱敏捷交易
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
Flink原理流程图简单记录
小程序+新零售,玩转行业新玩法!
随机推荐
Flask框架初学-05-命令管理Manager及数据库的使用
Flink原理流程图简单记录
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
Oracle迁移到瀚高之后,空值问题处理
Example: 036 is a prime number
Engineering drawing review questions (with answers)
The browser
C语言--环形缓存区
瑞能微计量芯片RN2026的实用程序
架构实战营模块三作业
idea中diagram使用
【学习笔记之菜Dog学C】动态内存管理
Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
单片机C语言->的用法,和意思
云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
C语言力扣第54题之螺旋矩阵。模拟旋转
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
Zabbix设置邮件告警+企业微信告警
多线程间的通信方式你知道几种?
Promise solves blocking synchronization and turns asynchronous into synchronous