Drill

ProblemsPython › patterns

Did they type the letters in order

easypatternsTwo pointersStringsPython

A command palette matches what someone typed against a command name: the letters have to appear in order, but not next to each other.

typed_in_order(typed: string, command: string) → bool

Solve it in the editor →

Where you start

def typed_in_order(typed: str, command: str) -> bool:
    

Worked examples

CallResult
typed_in_order("abc", "axbxc")True
typed_in_order("acb", "axbxc")False
typed_in_order("", "anything")True
typed_in_order("abc", "abc")True

Hint

One index walks the typed text, one walks the command. Advance the command index every step, and the typed index only when the two characters agree.

Reference solution in Python
def typed_in_order(typed: str, command: str) -> bool:
    i = 0
    for ch in command:
        if i < len(typed) and typed[i] == ch:
            i += 1
    return i == len(typed)

The same problem in another language

More patterns problems in Python