hackerrank的特别回文

时间:2019-02-12 04:37:59

标签: java arrays

我正在解决黑客等级问题 https://www.hackerrank.com/challenges/special-palindrome-again/problem

我无法通过测试用例仍然我的逻辑是正确的

我能够找到回文,但是我的代码找到了另一个未在解释中列出的回文,这会导致测试用例失败

List lstr=new ArrayList<>();

for(int i=0;i<s.length();i++)
{
    for(int j=i+1;j<=s.length();j++)
    {
       String str=new StringBuilder(s.substring(i, 
j)).reverse().toString();
    if(s.substring(i, j).equals(str) && s.substring(i, j).length()>1 )
    {
        lstr.add(str);
    }
}
return lstr.size()+s.length();
  

输入

     

5 asasd

     

特殊回文字符串{a,s,a,s,d,asa,sas}

     

输入

     

7 abcbaba

     

特殊回文字符串{a,b,c,b,a,b,a,bcb,bab,aba}但在上面   例如我的程序正在寻找abcba作为回文,这也导致   测试用例失败

1 个答案:

答案 0 :(得分:1)

再次为特殊字符串

我认为您没有计算每个可能的特殊字符串。 试试看,所有情况都可以通过。

 static long substrCount(int n, String s) {

        //automatically count each single character as a special string
        //thus, the default number of special strings is equal to
        //the length of the string
        int numOfSpecialStrings = s.length();

        //start iterating through the string
        for (int i = 0; i < s.length(); i++) {
            //to count all special strings, we
            //we will restart the counting
            //for each iteration.
            int numOfRepeat = 0;
            //this while loop takes care of the repetitive-character special strings
            while (i + 1 < s.length() && s.charAt(i) == s.charAt(i + 1)) {
                numOfRepeat++;
                i++;
            }
            //In a repetitive-character string the total of all
            //substrings can be calculated using the 
            //triangular numbers formula.
            numOfSpecialStrings += (numOfRepeat * (numOfRepeat + 1) / 2);

            //Now we have to take care of the special strings that
            //are mirrored, but they can be e.g aba but can also 
            //be aabaa and etc. How do we take care of these if 
            //we can have an infinite amount of these?

            //introduce a pointer that will take care of the substrings
            //with minimum size 3 - it will check the i-1,i and i+1.
            int pointer = 1;
            //So, while we do not violate the boundary conditions of the loop
            //we have increase the size of substring we are checking.
            //E.g if pointer = 1 and we check 
            // aba, b is i, so i-pointer is a and i+pointer is a.
            //We can see that a = a, so now we will expand our pointer
            // and increment the numberOfSpecialStrings and 
            //will now reach i-2, i, i+2 and so on.
            //This will go on until we either reach the end of the string, or the
            //condition is not satisfied. 
            while (i - pointer >= 0 && i + pointer < s.length() &&
                    s.charAt(i + pointer) == s.charAt(i - 1)
                    && s.charAt(i - pointer) == s.charAt(i - 1)) {
                numOfSpecialStrings++;
                pointer++;
            }
        }
        System.out.println(numOfSpecialStrings);
        return numOfSpecialStrings;
    }