AlgoInsight

DSA problem

Is Subsequence

EasyStringsPattern: Two Pointerstop75FreeGoogleAmazonFacebookPinterest

Given two strings s and t, return true if s is a subsequence of t, and false otherwise.

A subsequence of a string is a new string formed from the original by deleting some (possibly zero) characters without disturbing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde", while "aec" is not.

The check is order-sensitive: every character of s must appear in t in the same sequence, though not necessarily consecutively.

Examples

Example 1
Input: s = "abc", t = "ahbgdc"
Output: true
Explanation: Reading t left to right we find a, then b, then c in order, so "abc" is a subsequence.
Example 2
Input: s = "axc", t = "ahbgdc"
Output: false
Explanation: We can match a and c, but there is no x in t, so "axc" is not a subsequence.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointersdynamic-programming

Browse all DSA problems