Notes

Personal notes on various topics

View on GitHub

Is Subsequence

Problem Statement

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

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

Examples

Example 1:

Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"
Output: false

Constraints

Follow Up

Suppose there are a large number of incoming strings s1, s2, ..., sk where k >= 10^9, and you want to check for each if t contains it as a subsequence. How would you optimize your approach for this scenario?

Code Template

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        # Your code here
        pass

Solutions

Back to Problem List Back to Categories