Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 8, 2015 16:12
Show Gist options
  • Select an option

  • Save viveksyngh/9a9c1798d873973e5ffc to your computer and use it in GitHub Desktop.

Select an option

Save viveksyngh/9a9c1798d873973e5ffc to your computer and use it in GitHub Desktop.
Return the first index of needle in haystack otherwise -1
__author__ = 'Vivek'
def strStr(self, haystack, needle):
"""
:param:
:haystack: A string
:needle: A string
:return: index of the first ocurrence of needle in haystack
"""
if len(needle) == 0 or len(haystack)==0 :
return -1
elif needle == haystack :
return 0
else :
n = len(needle)
for i in range(0, len(haystack) - n + 1) :
if haystack[i : i + n] == needle :
return i
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment