Simple Python solution (not using filter() or remove()...)

  • 0

    Am I missing something?

    Why everybody keeps using fancy algorithm? Isn't this a simple question? ... Or I totally missed the point of this problem?

    class Solution:
      # @param haystack, a string
      # @param needle, a string
      # @return an integer
      def strStr(self, haystack, needle):
          for i in range(len(needle), len(haystack) + 1):
              if haystack[i - len(needle):i] == needle:
                  return i - len(needle)
          return -1

  • 0

    Same question! Someone one please help!

  • 3

    Your algorithm worst case is O(n2) while there are O(n) solutions.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.