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
  • 72,336
  • 29
  • 179
  • 389
user181218
  • 151
  • 2
  • 1
    It would appear that regardless, the for loop iterates the length of the string (the while loop inside doesn't matter--it still iterates over the length of the string). Thus it would appear this is an $O(n^2)$ problem since you have to call LookAndSay $n + 1$ times (if $n = 0$ then you call this exactly $1$ time) and each time it requires $i$ iterations, i.e. it requires $\sum_0^n (i + 1)$ iterations. – Jared Nov 20 '14 at 06:05
  • The while loop indeed doesn't matter. Here's the thing: I thought about O(n^2) from the same reasons you did, but it seems to me a very loose bound. After all, the string which the function works on at every iteration is generally longer than length(its previous one)+1 isn't it? So it's not a regular arithmetic progress. – user181218 Nov 20 '14 at 06:19
  • @Jared Please post answers as answers, not comments! – David Richerby Nov 20 '14 at 09:31
  • Could you replace the C++ with pseudocode? The details of the C++ don't seem relevant to the question itself and might be off-putting to people who aren't familiar with the language, which would decrease the future usefulness of the question. – David Richerby Nov 20 '14 at 09:33
  • We have a reference question that shows in detail how to perform such an analysis. Without an attempt of your own we can't help you with your specifid issues, so that reference is the best we can do for you know. (Note that problems have complexities, algorithms have (e.g. runtime) costs.) – Raphael Nov 20 '14 at 10:34
  • @DavidRicherby It's a comment not an answer because I'm speculating on a possible answer--I didn't devote enough time to be sure my comment was even correct (it's a first thought). – Jared Nov 20 '14 at 18:44
  • I'm on the same boat. "Elements of Programming Interviews" (https://books.google.com/books?id=y6FLBQAAQBAJ&pg=PA25&lpg=PA25&dq=look+and+say+sequence+complexity&source=bl&ots=AsJtdNy5xl&sig=UdY6COycwmPAN-2slG4FaQGhCjE&hl=en&sa=X&ei=PIIQVajvEsieNqaCgqAG&ved=0CEgQ6AEwBg#v=onepage&q=look%20and%20say%20sequence%20complexity&f=false) shows this is specially hard to calculate. On Page 221 it justifies that the complexity should be O(n2^n) based on the fact that the sequence on each iteration will be at most the double than for the previous number. – Christian Vielma Mar 23 '15 at 21:22

0 Answers0