Alternating Characters

  • + 0 comments

    First, O(N) is the same as O(N/2). The "O" means "order of", meaning that we ignore the coefficients to the terms (be they polynomial, log, or exponential).

    Second, this algorithm is no more efficient than the trivial one. It's also considerably less clear.

    While it's true you loop half the number of times, you do two comparisons each time through the loop. So, every character is examined twice (except the ones on the ends).

    Those who are timing out are doing something very inefficient, and are not O(N).

    Westerngravity has the right idea.