Drill

ProblemsGo › patterns

Did they type the letters in order

easypatternsTwo pointersStringsGo

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

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

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func typedInOrder(typed string, command string) bool {
	
}

Worked examples

CallResult
typedInOrder("abc", "axbxc")true
typedInOrder("acb", "axbxc")false
typedInOrder("", "anything")true
typedInOrder("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 Go
func typedInOrder(typed string, command string) bool {
	i := 0
	typedRunes := []rune(typed)
	for _, ch := range command {
	    if i < len(typedRunes) && typedRunes[i] == ch {
	        i++
	    }
	}
	return i == len(typedRunes)
}

The same problem in another language

More patterns problems in Go