Is It Hidden Inside

EasyTwo Pointers≈ 15 min

A word is hidden inside a text when you can find its letters in the text in the same order, though not necessarily next to each other: you may skip any letters of the text in between. Given the strings word and text, return true if word is hidden inside text, and false if not.

Each letter of the text can be used for only one letter of the word. An empty word is hidden inside any text.

Examples

text
word = "ace", text = "abcde"        →  true
word = "aec", text = "abcde"        →  false
word = "", text = "anything"        →  true
word = "cat", text = "concatenate"  →  true

In the second example the text has an a, an e and a c, but the c comes before the e.

Constraints

  • Each string holds between 0 and 20,000 characters.
  • Aim for O(n + m): read each string once.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.