1

I am trying to figure out what the time complexity is for a "Look and Say" sequence generator which receives an integer n and outputs the nth term in the look and say sequence.

I'm looking at the below code. It is a tail recursion, but I can't get the summation properly. Can someone please provide an explanation along with the answer?

string LookAndSay(int n)
{
    string ret;
    if(n == 0)
    {
        return "1"; 
    }
    string s = LookAndSay(n-1);
    for(auto it = s.begin(); it != s.end(); it++)
    {
        char curDigit = *it;
        int count = 0;
        while(it != s.end() && *it == curDigit)
        {
            count++;
            it++;
        }
        it--;
        std::stringstream ss;
        ss << count;
        ret += ss.str();
        ret += curDigit;
    }
    return ret;
}
Raphael
  • 73,212
  • 30
  • 182
  • 400
user181218
  • 151
  • 2

0 Answers0