Skip to content

Commit

Permalink
adds string subsequence of another string
Browse files Browse the repository at this point in the history
  • Loading branch information
esl4m committed May 28, 2022
1 parent 532a6e4 commit 9aab4f7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions leetcode/string_subs_of_second.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Iterative Python program to check if a string is subsequence of another string

# Returns true if str1 is a subsequence of str2
# m is length of str1, n is length of str2
def is_subSequence(str1, str2, m, n):
j = 0 # Index of str1
i = 0 # Index of str2

# Traverse both str1 and str2
# Compare current character of str2 with
# first unmatched character of str1
# If matched, then move ahead in str1

while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1

# If all characters of str1 matched, then j is equal to m
return j == m


# Driver Program

str1 = "gksrek"
str2 = "geeksforgeeks"
m = len(str1)
n = len(str2)

print("Yes" if is_subSequence(str1, str2, m, n) else "No")

0 comments on commit 9aab4f7

Please sign in to comment.