Teaching Kids Programming: Videos on Data Structures and Algorithms
Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not).
Example 1:
Input: s = “abc”, t = “ahbgdc”
Output: trueExample 2:
Input: s = “axc”, t = “ahbgdc”
Output: falseConstraints:
0 <= s.length <= 100
0 <= t.length <= 104
s and t consist only of lowercase English letters.Follow up: Suppose there are lots of incoming s, say s1, s2, …, sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
Is Subsequence Algorithm via Two Pointer
We have learned the Two Pointer Algorithm (Teaching Kids Programming – Is Subsequence Algorithm via Two Pointer) to check if a string is a subsequence of another string.
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):
return False
i = 0
j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
else:
j += 1
return i == len(s)
The time complexity is O(N) where N is the length of the longer string t.
Is Subsequence Algorithm via Recursion + Greedy
We can define a Recursive Function takes take two parameters, the i and j indicating the current position for s and t respectively. Then we have two cases: if s[i] == s[t] or s[i] != s[t]. When we have a match, we can advance two pointers. Otherwise, we have to move the j pointer to next.
The idea is same as above two pointer approach, however implemented in Recursion.
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):
return False
def dfs(i, j):
if i == len(s):
return True
if j == len(t):
return False
if s[i] == t[j]:
return dfs(i + 1, j + 1)
return dfs(i, j + 1)
return dfs(0, 0)
We can check from the end backwards as well:
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):
return False
def dfs(i, j):
if i < 0:
return True
if j < 0:
return False
if s[i] == t[j]:
return dfs(i - 1, j - 1)
return dfs(i, j - 1)
return dfs(len(s) - 1, len(t) - 1)
The time complexity is O(N) where N is the length for the longer string t.
String Subsequence Algorithms:
- Teaching Kids Programming – Is Subsequence Algorithm via Recursion (Greedy)
- Teaching Kids Programming – Is Subsequence Algorithm via Two Pointer
- The Subsequence Algorithm for Two Strings – How to Check if a String is Subsequence of Another?
- GoLang Function of Checking Is Subsequence
- Algorithms to Check if a String is a Subsequence String of Another String
- in Python, you can do this in Pythonic way: Python Function to Check Subsequence
–EOF (The Ultimate Computing & Technology Blog) —
775 wordsLast Post: Teaching Kids Programming - How to Make Flashing Lights on Microbit?
Next Post: Teaching Kids Programming - Draw a Tree in Python using Turtle Graphics (Recursion)