? Companies that asked this problem: Accolite, Tesco, Google
Problem Formulation
Description
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
Given two strings str1 and str2, check if str1 is a subsequence of str2.
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).
constraints:
0 <= str1.length <= 1000 <= str2.length <= 104str1andstr2consist only of lowercase English letters.
Example
Input: str1 = "abc", str2 = "ahbgdc" Output: True Input: str1 = "axc", str2 = "ahbgdc" Output: False
Border Cases
- If
str1andstr2are both empty, then Output βTRUE, as empty string a subsequence of another empty string. - If
str1β empty andstr2β not empty, then Output βTRUE, as empty string is also a subsequence of any given string. - If
str1β not empty andstr2β empty, then Output βFALSE, as non-empty string cannot be subsequence of empty string.
Input: str1 = "", str2 = "" Output: True Input: str1 = "", str2 = "ahbgdc" Output: True Input: str1 = "abc", str2 = "" Output: False
Proposed Solution Overview
Traverse through each character in the given strings. There are two cases when you do so:
- The current characters of both strings are equal. So, move on to the next next index/character of
str1andstr2. - Current characters of both strings are not equal. So, move on to the next index/character of
str2. However the index ofstr1remains fixed in this case since the matching character was not found.
The above process is repeated until on of the following criteria’s are satisfied:
- All characters of
str1are found to be present instr2. The length ofstr1and the current value ofindex_str1will be equal in this case. This denotes that we successfully found the subsequence within the given string. In other words,str1is a subsequence ofstr2. - All characters of
str2have been traversed. This means that either the last character ofstr1andstr2are equal orstr2is not a subsequence ofstr1.
Diagrammatic representation:

Solution
def isSubSequence(str1, str2):
len_str1 = len(str1)
len_str2 = len(str2)
index_str1 = 0
index_str2 = 0
# Traverse both str1 and str2
while index_str1 < len_str1 and index_str2 < len_str2:
# Compare current character of str2 with str1
if str1[index_str1] == str2[index_str2]:
# If matched, then move to next character in str1
index_str1 = index_str1 + 1
index_str2 = index_str2 + 1
return index_str1 == len_str1
val_1 = 'abc'
val_2 = 'ahbgdc'
print(isSubSequence(val_1, val_2))
Output:
True
Code Explanation:
len_str1andlen_str2store the length ofstr1andstr2respectively.index_str1andindex_str2are used to store the indices of each character ofstr1andstr2respectively.- The
whileloop is used to traverse through the strings until a match is found or all the indices of str2 have been traversed in case of no match.- The
if statementcompares the current character ofstr1andstr2.- In case a match is found, the index of the next character in
str1is taken into account.
- In case a match is found, the index of the next character in
- The value of
index_str1is incremented at every iteration to traverse through all the letters available instr1until the subsequence is found.
- The
- Finally, if the subsequence was found the value stored by
index_str1will be equal to the length ofstr1.

Dry Run:
The following table illustrates the operation at each iteration within the while loop until the match is found.
