【问题标题】:Special Palindrome from hackerrank来自hackerrank的特殊回文
【发布时间】:2019-02-12 04:37:59
【问题描述】:

我正在解决一个黑客等级问题 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 阿布巴巴

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

【问题讨论】:

标签: java arrays


【解决方案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;
    }

【讨论】:

  • 一个优雅的解决方案,但特殊的回文检查缺少一个关键条件,即当前字符应该不同于前一个字符:s.charAt(i) != s.charAt(i-1)。如果没有这个条件,你会将aaa 算作回文。
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 2011-09-22
  • 2011-01-16
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多