当前位置:网站首页>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);}}
边栏推荐
- [QNX Hypervisor 2.2 User Manual] 10.3 vdev gic
- Example 037: Sorting
- 【Playwright测试教程】5分钟上手
- 安全至上:落地DevSecOps最佳实践你不得不知道的工具
- 2022年茶艺师(中级)考试试题模拟考试平台操作
- FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
- C语言--环形缓存区
- C语言力扣第54题之螺旋矩阵。模拟旋转
- 异步编程解决方案 Generator生成器函数、iterator迭代器、async/await、Promise
- sql有关问题,小时粒度,找到前一个小时内的数据
猜你喜欢
Web APIs BOM - operating browser: swiper plug-in
Zabbix set up email alert + enterprise WeChat alert
C程序编译和预定义详解
系统太多,多账号互通如何实现?
APP电商如何快速分润分账?
5. Scrapy middleware & distributed crawler
mpf5_定价Bond_yield curve_Spot coupon_duration_有效利率_连续复利_远期_Vasicek短期_CIR模型Derivatives_Tridiagonal_ppf
The idea of the diagram
融云「音视频架构实践」技术专场【内含完整PPT】
Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
随机推荐
Security First: Tools You Need to Know to Implement DevSecOps Best Practices
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型
Utilities of Ruineng Micrometer Chip RN2026
Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
Priority_queue element as a pointer, the overloaded operators
Parquet encoding
2022G1工业锅炉司炉考试练习题及模拟考试
Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
Promise solves blocking synchronization and turns asynchronous into synchronous
STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
sqoop ETL工具
TOML配置文件格式,YAML最有力的竞争者
ant-design的Select组件采用自定义后缀图标(suffixIcon属性)时,点击该自定义图标没有反应,不会展示下拉菜单的问题
In the season of going overseas, the localization of Internet tips for going overseas
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
系统太多,多账号互通如何实现?
Snake game bug analysis and function expansion
Simple sorting (summer vacation daily question 14)