Java 如何返回一个整数来指示回文的不同子字符串的数量?

Java 如何返回一个整数来指示回文的不同子字符串的数量?,java,Java,有人能帮我写代码吗?因为我不明白我做错了什么。谢谢大家! 下面是我的代码: public class Solution{ //code I need Help with: public static int palindromeCount(String s) { int count = 0; for (int i = 0; i < s.length(); i++) { for (int j = 0; j < s.length(); j++) {

有人能帮我写代码吗?因为我不明白我做错了什么。谢谢大家!

下面是我的代码:

public class Solution{

//code I need Help with:

public static int palindromeCount(String s) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        for (int j = 0; j < s.length(); j++) {
            if (s.charAt(i) == s.charAt(j)) {
            }
            count++;
        }
        break;
    }

    return count;
}

//Given and can't change:

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    final String fileName = System.getenv("OUTPUT_PATH");
    BufferedWriter bw = null;
    if (fileName != null) {
        bw = new BufferedWriter(new FileWriter(fileName));
    } else {
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
    }

    int res;
    String s;
    try {
        s = in.nextLine();
    } catch (Exception e) {
        s = null;
    }

    res = palindromeCount(s);
    bw.write(String.valueOf(res));
    bw.newLine();

    bw.close();
    }
  }
公共类解决方案{
//代码我需要以下方面的帮助:
公共静态整型回文计数(字符串s){
整数计数=0;
对于(int i=0;i
输出: 函数必须返回一个整数,表示回文的不同子字符串的数量

Ex输入:

s=aabaa

输出:

五,

原因:a,aa,AAAA,aba,b


子字符串“a”出现4次,但我们正在寻找不同的子字符串。那么它只算一次。类似地,字符串“aa”出现两次,但作为一个不同的回文进行计数。

您可以将每个字符串添加到一个集合中(不能包含重复项),而函数末尾的只返回集合的大小


以下是如何使用集合消除重复项的实现

import java.util.*; //Needed import to use the Set

  public static void main(String[] args)
  {
    String s = "aabaa";  //Test String - Has 5 palindroms: a, aa, aabaa, aba, b 
    HashSet<String> countSet = new HashSet<String>();  //Use a Set since it will elimate duplicate values
    for (int i = 0; i < s.length(); i++) { 
        for (int j = i+1; j <= s.length(); j++) {  //check every substring possibility
            if(isPalindrom( s.substring(i,j).toCharArray() ) ) //If the substring is a palindrom...
            {
              countSet.add(s.substring(i,j)); //... add it to our Set.  Duplicates will be ignored because it is a Set
            }
        }
    }

    System.out.println("HERE: " + countSet.size());  //The number of palindroms.  5 in this case
  }

  //Fuction that checks if a char[] is a palindrom
  public static boolean isPalindrom(char[] word){
    int i1 = 0;
    int i2 = word.length - 1;
    while (i2 > i1) {
        if (word[i1] != word[i2]) {
            return false;
        }
        ++i1;
        --i2;
    }
    return true;
  }
import java.util.*//需要导入才能使用集合
公共静态void main(字符串[]args)
{
String s=“aabaa”//测试字符串-有5个回文:a、aa、aabaa、aba、b
HashSet countSet=new HashSet();//使用集合,因为它将删除重复的值
对于(inti=0;i
hmm我确实尝试过,但我不知道如何实现它。@R..您需要一个双嵌套for循环-一个用于字符串长度,另一个用于向下遍历字符串…基本上
for(潜在子字符串的长度起始长度1和此字符串的结束长度){for(字符串的每个潜在起始字符){String potential=s.substring(…);if(isAlindrome(potential))set.add(potential);}}返回potential.size();
-在nutshell@corsiKa在我写这段代码之前我确实试过了,但是没有用。谢谢你的帮助!!