Skip to content

visual walkthrough

Is Subsequence

EasySame DirectionReported at: AmazonGoogleAdobe

Greedy matching with two pointers over two strings.

Solve on LeetCode

The idea

A subsequence keeps the order of letters but may skip some. So greedily match: keep a pointer on the next letter of s you still need and move along t, advancing it whenever t shows that letter.

If you reach the end of s, every letter was found in order.

Complexity

approachtimespace
Two pointersO(n)O(1)

More walkthroughs