I need to write an algorithm that reads keyboard stream until it gets a correct password.
like if the password is ababac,
and the input is abababa, it means so far it read ababa and now it waits for a c to get unlocked, if an f comes in instead of a c, then it restarts its process.
it easily can be done in O(n^2), but my teacher want us to do it in O(n) W.C, can it get done in this complexity?!
You could build an automaton for it.
For this approach – you can use Aho–Corasick – create the data automaton for the string, and start feeding it with the input text, until you reach its end [don’t accept] or you reached an accepting state in the automaton.
Aho-Corasick is linear in the pattern [password] and input size, so you get
O(m+n)