Did they type the letters in order
A command palette matches what someone typed against a command name: the letters have to appear in order, but not next to each other.
- Every character of the typed text must appear in the command, in the same order.
- Characters may be spread out — gaps in the command are fine.
- Typing nothing matches every command.
- Matching is exact, character for character; nothing is case-folded here.
TypedInOrder(typed: string, command: string) → bool
C# 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.
Where you start
public bool TypedInOrder(string typed, string command) {
}
Worked examples
| Call | Result |
|---|---|
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 C#
public bool TypedInOrder(string typed, string command) {
int i = 0;
foreach (var ch in command) {
if (i < typed.Length && typed[i] == ch) i++;
}
return i == typed.Length;
}