0
$\begingroup$

Suppose I have a string that goes like "abcabcdeaabcdef", where each character is either one more than the previous one or a.

If the character at position i is "c", I know that it is preceded by "ab".

I want to find the number and length of all sequences (substrings that contain sequential characters). For example, the above string would give "abc", "abcde", "a", "abcdef".

Is there a more efficient algorithm to do this than simply doing a O(n) search over the string?

asked Jan 17, 2017 at 15:59
$\endgroup$
4
  • 2
    $\begingroup$ Under usual assumptions, taking $o(n)$ time means that your algorithm can't read the entire string, since the correct answer depends on all characters, this means it will fail on some input. Intuitively, you can't get away with not reading the input unless the answer is local to some subset of it or some additional hypotheses holds. $\endgroup$ Commented Jan 17, 2017 at 16:39
  • 1
    $\begingroup$ Why isn't "ab" a valid sequence? $\endgroup$ Commented Jan 17, 2017 at 17:01
  • $\begingroup$ You are going to have to read the characters to get the sequence $\endgroup$ Commented Jan 17, 2017 at 21:33
  • $\begingroup$ Worst case? an, with no way to know without checking every character. $\endgroup$ Commented Jan 18, 2017 at 19:50

2 Answers 2

2
$\begingroup$

You cant create an algoithm to do this in the worst case. Even if we read right to left and if we assume we can do some level of jumping (e.g if we see an f we can jump 6 spaces left) we still have to, in the worst case (the string of a's), read over the entire string. This can never be done in $o(n)$ time. The right to left with skipping will run in $O(m)$ time where $m$ is the number of increasing subsequences, and $m$ is $O(n)$.

answered Jan 18, 2017 at 15:05
$\endgroup$
0
$\begingroup$

Hmm i don't think so.Let's suppose there exists algorithm which does less then (n-2) comprehensions and gives a correct answer. (we assume we never get empty input and our input always starts with a thats why n-2)

Let's define 2 inputs A = aaaaaaa... and B = aaa....b...a.aaa

Of course f(a) = n and f(b) = n - 1 where f is our algorithm.

Let's play a game with our Alghoritm:

Let's define function g(x) = 1 if input[x] = a, 0 otherwise.

Each call to g is one comprehension. We can easily construct 2 inputs with different numer of sequences which would have identical result of n-2 calls of g

answered Jan 17, 2017 at 16:41
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.