LeetCode - Algorithms
  • Introduction
  • 1. Two Sum
  • 2. Add Two Numbers
  • 3. Longest Substring Without Repeating Characters
  • 4. Median of Two Sorted Arrays
  • 5. Longest Palindromic Substring
  • 6. ZigZag Conversion
  • 7. Reverse Integer
  • 8. String to Integer (atoi)
  • 9. Palindrome Number
  • 10. Regular Expression Matching
  • 11. Container with Most Water
  • 12. Integer to Roman
  • 13. Roman to Integer
  • 14. Longest Common Prefix
  • 15. 3Sum
  • 16. 3Sum Closest
  • 17. Letter Combinations of a Phone Number
  • 18. 4Sum
  • 19. Remove Nth Node From End of List
  • 20. Valid Parentheses
  • 21. Merge Two Sorted Lists
  • 22. Generate Parentheses
  • 23. Merge k Sorted Lists
  • 24. Swap Nodes in Pairs
  • 25. Reverse Nodes in k-Group
  • 26. Remove Duplicates from Sorted Array
  • 27. Remove Element
  • 28. Implement strStr()
  • 29. Divide Two Integers
  • 30. Substring with Concatenation of All Words
  • 31. Next Permutation
  • 32. Longest Valid Parentheses
  • 33. Search in Rotated Sorted Array
  • 34. Search for a Range
  • 35. Search Insert Position
  • 36. Valid Sudoku
  • 37. Sudoku Solver
  • 38. Count and Say
  • 39. Combination Sum
Powered by GitBook
On this page
  • Problem
  • Analysis
  • Code

28. Implement strStr()

Problem

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

Related Topics:

Two Pointers String

Analysis

Sunday algorithm,核心思想:尽可能多的跳过字符,减少判断。

当主串与模式串不匹配时,匹配点变更为主串上,模式串末尾的下一位所对应的字符。然后在模式串上寻找该字符(从后往前寻找,确保不会出现遗漏的情况):

  • 若存在,以模式串上对应的字符为标准,移动模式串到匹配点。

  • 若不存在,以模式串头为标准,移动模式串到匹配点。

由于需要在模式串上寻找字符及其位置,所以可以使用数组来存储每个字符所对应的位置。从前往后遍历模式串进行存储,自然后面相同的字符会覆盖前面的,即符合在模式串上寻找的条件。

Code

class Solution {

    fun strStr(haystack: String, needle: String): Int {

        if (needle.isEmpty()) {
            return 0
        }

        val charIndex = IntArray(256)
        charIndex.fill(-1)
        for (i in needle.indices) {
            charIndex[needle[i].toInt()] = i
        }

        var index = 0
        val len = haystack.length - needle.length
        var hi: Int
        var ni: Int
        while (index <= len) {

            hi = index
            ni = 0
            while (hi < haystack.length && ni < needle.length && haystack[hi] == needle[ni]) {
                hi++
                ni++
            }

            when {
                ni == needle.length -> return index
                index >= len -> return -1
                else -> index += (needle.length - charIndex[haystack[index + needle.length].toInt()])
            }
        }

        return -1
    }
}
Previous27. Remove ElementNext29. Divide Two Integers

Last updated 6 years ago