Created
August 8, 2015 16:12
-
-
Save viveksyngh/9a9c1798d873973e5ffc to your computer and use it in GitHub Desktop.
Return the first index of needle in haystack otherwise -1
This file contains hidden or 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
| __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