Wrong test case: input 1, expected 1, got 11?


  • 0
    C

    Wrong test case?

    class Solution {
    public:
    string countAndSay(int n) {

        string ret;
        string inp=to_string((unsigned long long)n);
        
        int i=0, count=0;
        while(i<inp.size())
        {
            char say=inp[i];
            count=1;
            
            int j=NextNoneSay(inp,i+1, say);
            if(j==-1)
            {
                //reached the end
                ret.append(to_string(count));
                ret.append(1,say);
                break;
            }
            else
            {
            count=count+j-i-1;
                ret.append(to_string(count));
                ret.append(1,say);
                i=j;
            }
        }
        
        return ret;
        
    }
    
    int NextNoneSay(const string &s, int start, char say)
    {
        int i=start;
        bool found=false;
        while(i<s.size())
        {
            if(s[i] !=say) { found = true; break; }
            i++;
        }
        
        if(found) return i;
        else return -1;
    }
    

    };


  • 0
    L

    Note that "1" is the 1st sequence.


  • 0
    C

    It should be "one1" -> "11", according to the problem, right? but the test site expected "1".


  • 2
    E

    I think you misunderstand the question. What you are doing is just one step of the problem: get the "count and say" string. However, the problem asks a series of them, and output the nth element of the series. By "a series of them", I mean each of the element in the series is the "count and say" string of the last element (the last "count and say" string). So you are going to take the output of your function when n=1, as the input of your function when n=2, and then put the output as the input of your function as n=3, ...etc.

    Take a look at the example of the problem. The series is 1, 11, 21, 1211, 111221 ..., so when n=1, you should give 1. When n=2, you should give 11, etc.


  • 0
    C

    Got you. Thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.