LeetCode: Is Subsequence

Given two strings, determine if one is a subsequence of the other

Posted on LeetCode Python

See original problem on leetcode.com

Difficulty: Easy

Solution

Create a pointer i for walking through s.

Walk through each character c in t. If c matches s[i], increment i by 1. If we have reached the end of s, then a subsequence is found.

Runtime

0 ms | Beats 100.00%

class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0:
return True
s_index = 0
for c in t:
if s[s_index] == c:
s_index += 1
if s_index >= len(s):
return True
return False