实现 strStr()

题目描述:实现 strStr() 函数。

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-strstr/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:穷举法
  • 首先,如果needle为空,直接返回0;如果 haystack 为空 或者 haystack 的长度 小于 needle 的长度,直接返回-1;
  • 否则,从 haystack 的第一位开始跟 needle 进行匹配,如果匹配不上,则往后继续遍历haystack,直到遍历完成,就能得到结果。

说明:该方法效率比较差。

解法二:KMP算法

首先,构造一个next数组,先计算出下一次跳转的位置,然后遍历按照next数组的位置将原串和匹配串进行匹配。

  1. import com.google.common.base.Strings;
  2. public class LeetCode_028 {
  3. /**
  4. * 穷举法
  5. *
  6. * @param haystack
  7. * @param needle
  8. * @return
  9. */
  10. public static int strStr(String haystack, String needle) {
  11. if (needle == null || needle.length() == 0) {
  12. return 0;
  13. }
  14. if (haystack == null || haystack.length() == 0 || haystack.length() < needle.length()) {
  15. return -1;
  16. }
  17. int first = 0;
  18. while (first < haystack.length()) {
  19. int matchCount = 0;
  20. for (int i = 0; i < needle.length() && (i + first) < haystack.length(); i++) {
  21. if (needle.charAt(i) == haystack.charAt(i + first)) {
  22. matchCount++;
  23. } else {
  24. break;
  25. }
  26. }
  27. if (matchCount == needle.length()) {
  28. return first;
  29. } else {
  30. first++;
  31. }
  32. }
  33. return -1;
  34. }
  35. /**
  36. * KMP算法
  37. *
  38. * @param haystack 原串
  39. * @param needle 匹配串
  40. * @return
  41. */
  42. public static int strStr2(String haystack, String needle) {
  43. if (Strings.isNullOrEmpty(needle)) {
  44. return 0;
  45. }
  46. // 分别获取原串和匹配串的长度
  47. int haystackLength = haystack.length(), needleLength = needle.length();
  48. // 原串和匹配串前面都加一个空格,使其下标从1开始
  49. haystack = " " + haystack;
  50. needle = " " + needle;
  51. char[] haystackList = haystack.toCharArray();
  52. char[] needleList = needle.toCharArray();
  53. // 构建 next 数组,数组长度为匹配串的长度(next 数组是和匹配串相关的)
  54. int[] next = new int[needleLength + 1];
  55. // 构造过程 i = 2, j = 0 开始,i 小于等于匹配串长度【构造 i 从 2 开始】
  56. for (int i = 2, j = 0; i <= needleLength; i++) {
  57. // 匹配不成功的话,j = next[j]
  58. while (j > 0 && needleList[i] != needleList[j + 1]) {
  59. j = next[j];
  60. }
  61. // 匹配成功的话,先让 j++
  62. if (needleList[i] == needleList[j + 1]) {
  63. j++;
  64. }
  65. // 更新 next[i],结束本次循环, i++
  66. next[i] = j;
  67. }
  68. // 匹配过程,i = 1, j = 0 开始,i 小于等于原串长度【匹配 i 从 1 开始】
  69. for (int i = 1, j = 0; i <= haystackLength; i++) {
  70. // 匹配不成功 j = next[j]
  71. while (j > 0 && haystackList[i] != needleList[j + 1]) {
  72. j = next[j];
  73. }
  74. // 匹配成功的话,先让 j++,结束本次循环后 i++
  75. if (haystackList[i] == needleList[j + 1]) {
  76. j++;
  77. }
  78. // 整一段都匹配成功,直接返回下标
  79. if (j == needleLength) {
  80. return i - needleLength;
  81. }
  82. }
  83. return -1;
  84. }
  85. public static void main(String[] args) {
  86. System.out.println(strStr("mississippi", "issi"));
  87. System.out.println(strStr2("mississippi", "issi"));
  88. }
  89. }

【每日寄语】 在最美的年华,做最喜欢的事情,别辜负了美好时光,借时光之手,暖一处花开,借一方晴空,拥抱梦想。