Created
February 12, 2017 10:40
-
-
Save girish3/c72811be1ec3e3d0668a9a2e7a2b1433 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
# Here is the working code of the naive approach. | |
def bruteSearch(W, T): | |
# edge case check | |
if W == "": | |
return -1 | |
# getting the length of the strings | |
wordLen = len(W) | |
textLen = len(T) | |
# i is the index of text T from where we will start comparing the | |
# the word W | |
i = 0 | |
# length of the subtext has to be equal to the length of the word, | |
# so no need to check beyond (textLen - wordLen + 1) | |
while i < (textLen - wordLen + 1): | |
# we set match to false if we find a mismatch | |
match = True | |
for j in range(wordLen): | |
if W[j] != T[i + j]: | |
# A mismatch | |
match = False | |
break | |
if match: | |
# We found a match at index i | |
print "There is a match at " + str(i) | |
# incrementing i is like shifting the word by 1 | |
i += 1 | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment