Created
February 27, 2018 15:42
-
-
Save FeraruSilviuMarian/4b3211960b532eab814108f1598a5e91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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