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


  • 0
    G

    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
    Z

    Same question! Someone one please help!


  • 3
    M

    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.