当前位置:网站首页>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);}}
边栏推荐
- 为什么用Selenium做自动化测试
- 出海季,互联网出海锦囊之本地化
- Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)
- priority_queue元素为指针时,重载运算符失效
- 织梦内核电动伸缩门卷闸门门业公司网站模板 带手机版【站长亲测】
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- Example 039: Inserting elements into an ordered list
- Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
- Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
- SAP SD模块前台操作
猜你喜欢
DDTL: Domain Transfer Learning at a Distance
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
uni-app 从零开始-基础模版(一)
Deep Learning (3) Classification Theory Part
Example 040: Reverse List
STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
Web APIs BOM - operating browser: swiper plug-in
【Playwright测试教程】5分钟上手
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
随机推荐
STM8S-----选项字节
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
2022.8.3-----leetcode.899
lombok注解@RequiredArgsConstructor的使用
Instance, 038: the sum of the diagonal matrix
Priority_queue element as a pointer, the overloaded operators
云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
Engineering drawing review questions (with answers)
Utilities of Ruineng Micrometer Chip RN2026
实例037:排序
Example: 036 is a prime number
Deep Learning (3) Classification Theory Part
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
【原创】启动Win10自带的XPS/OXPS阅读器
pytorch applied to MNIST handwritten font recognition
多线程间的通信方式你知道几种?
如何在MySQL中的数据库下删除所有的表
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
The browser
实例040:逆序列表