当前位置:网站首页>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);}}
边栏推荐
- 香港服务器有哪些常用的型号
- Flask Framework Beginner-05-Command Management Manager and Database Use
- 5. Scrapy middleware & distributed crawler
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- 实例039:有序列表插入元素
- Flask框架初学-05-命令管理Manager及数据库的使用
- 出海季,互联网出海锦囊之本地化
- Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
- pytorch applied to MNIST handwritten font recognition
- Zabbix设置邮件告警+企业微信告警
猜你喜欢

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

Zabbix set up email alert + enterprise WeChat alert

小程序+新零售,玩转行业新玩法!

单片机C语言->的用法,和意思

Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).

Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)

What is SVN (Subversion)?

阿里云国际版基于快照与镜像功能迁移云服务器数据

ant-design的Select组件采用自定义后缀图标(suffixIcon属性)时,点击该自定义图标没有反应,不会展示下拉菜单的问题

在更一般意义上验算移位距离和假设
随机推荐
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
小程序+新零售,玩转行业新玩法!
在更一般意义上验算移位距离和假设
实例036:算素数
Web APIs BOM - operating browser: swiper plug-in
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
如何在MySQL中的数据库下删除所有的表
脚手架内容详解分析
Zabbix set up email alert + enterprise WeChat alert
实例041:类的方法与变量
priority_queue元素为指针时,重载运算符失效
实例038:矩阵对角线之和
浏览器存储
QNX Hypervisor 2.2 user manual] 10.1 gm vdev options
lombok注解@RequiredArgsConstructor的使用
出海季,互联网出海锦囊之本地化
Simple record of Flink principle flow chart
DDTL: Domain Transfer Learning at a Distance
What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
WPE详细教程