美文网首页
查找最大回文子串

查找最大回文子串

作者: 开包辣条压压惊_bda1 | 来源:发表于2017-11-19 21:38 被阅读0次

package test;import java.util.Stack;

/** * 查找最大回文字串 * 

 * @author Administrator *

 */

public class FindMaxRoundString {

public static void main(String[] args) {

System.out.println(findMaxRoundString("ababcbadd1234321d"));

}

public static String findMaxRoundString(String source) {

int maxRoundStrLength = 0; // 当前检测到的最大回文

String maxRoundStr="";

String tempStr;

for (int i = 0; i < source.length(); i++) {

if (source.length() - i > maxRoundStrLength) {

for (int j = i + 1; j <= source.length(); j++) {

tempStr = source.substring(i, j);

if (checkIsRound(tempStr)) {

if(tempStr.length()>maxRoundStrLength) {

maxRoundStr = tempStr;

maxRoundStrLength = tempStr.length();

}

 }

}

}

return maxRoundStr;

}

public static boolean checkIsRound(String str) {

Stackstack = new Stack();

char[] chars = str.toCharArray();

for (int i = 0; i < str.length(); i++) {

stack.push(String.valueOf(chars[i]));

}

StringBuffer dest = new StringBuffer();

while (!stack.isEmpty()) {

dest.append(stack.pop());

}

return str.equals(dest.toString());

}

}

输出:d1234321d

相关文章

  • 查找最大回文子串

    package test;import java.util.Stack; /** * 查找最大回文字串 * * @...

  • 字符串hash

    兔子和兔子 最大回文子串 kmp周期

  • 最大回文子串

    1.暴力求解(Brute Force) O(n^3) 2.动态规划(Dynamic planning) O(n^2...

  • 最长回文子串

    最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。

  • 最长回文子串

    给定一个字符串,求它的最长会问子串的长度 方法一:中心扩展 依次遍历字符串,向两边查找回文,在查找时要注意偶数回文...

  • LeetCode练手系列——最长回文子串

    题目:最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。 示例 ...

  • 最长回文子串

    最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 分析:参考...

  • LeetCode-5-最长回文子串

    LeetCode-5-最长回文子串 题目 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长...

  • Leetcode 5 最长回文子串

    最长回文子串 题目 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例...

  • 最长回文子串

    最长回文子串 题目 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000 摘要 ...

网友评论

      本文标题:查找最大回文子串

      本文链接:https://www.haomeiwen.com/subject/etopvxtx.html