Skip to content

Instantly share code, notes, and snippets.

@FeraruSilviuMarian
Created February 27, 2018 15:42
Show Gist options
  • Save FeraruSilviuMarian/4b3211960b532eab814108f1598a5e91 to your computer and use it in GitHub Desktop.
Save FeraruSilviuMarian/4b3211960b532eab814108f1598a5e91 to your computer and use it in GitHub Desktop.
def isSubstring(substring, string):
"""
substring: string
string: string
returns true if substring is substring of string e.g. 'cat' is substring of 'something cat something'
"""
si = 0 # substring indexer
for i in range(len(string)):
if string[i] == substring[si]:
si += 1 # notice, advancing the substring indexer each time we find a match
if si == (len(substring)): # if the substring indexer reaches the length of the substring, it is a substring
return True
else: # also notice, we reset the substring indexer if we don't find a match, thus, only consecutive matches count
si = 0
return False
s = 'something'
s2 = 'cat'
phrase = 'once uppon a time something happened'
print(isSubstring(s, phrase))
print(isSubstring(s2, phrase))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment