Skip to content

Instantly share code, notes, and snippets.

@girish3
Created February 12, 2017 10:40
Show Gist options
  • Save girish3/c72811be1ec3e3d0668a9a2e7a2b1433 to your computer and use it in GitHub Desktop.
Save girish3/c72811be1ec3e3d0668a9a2e7a2b1433 to your computer and use it in GitHub Desktop.
# 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