본문 바로가기
Python/PS in Python

LeetCode 28. Implement strStr() - Python

by Air’s Big Data 2020. 3. 28.

LeetCode28. Implement strStr()

 

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll" Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba" Output: -1

 

 

Solution

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if haystack.find(needle) >= 0:
            return haystack.find(needle)
        else:
            return -1


댓글