Drill

ProblemsJavaScript › patterns

Did they type the letters in order

easypatternsTwo pointersStringsJavaScript

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

Solve it in the editor →

Where you start

function typedInOrder(typed, command) {
  
}

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 JavaScript
function typedInOrder(typed, command) {
  let i = 0;
  for (const ch of command) {
    if (i < typed.length && typed[i] === ch) i += 1;
  }
  return i === typed.length;
}

The same problem in another language

More patterns problems in JavaScript