实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
一、暴力对比法
Python:
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle :
return 0
n1 = len(haystack)
n2 = len(needle)
if n1 < n2:
return -1
def helper(i):
haystack_p = i
needle_q = 0
while needle_q < n2:
if haystack[haystack_p] != needle[needle_q]:
return False
else:
haystack_p += 1
needle_q += 1
return True
for i in range(n1 - n2 + 1):
if helper(i):
return i
return -1
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
for i in range(len(haystack) - len(needle)+1):
if haystack[i:i+len(needle)] == needle:
return i
return -1
Java:
class Solution
{
public int strStr(String S, String T)
{
int n1 = S.length();
int n2 = T.length();
if (n1 < n2)
return -1;
else if ( n2 == 0)
return 0;
for (int i = 0; i < n1 - n2 + 1; i++ )
{
if (S.substring(i, i+n2).equals(T))
return i;
}
return -1;
}
}
KMP 算法(参考:https://kenby.iteye.com/blog/1025599)
python
class Solution:
def strStr(self, t, p):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not p : return 0
_next = [0] * len(p)
def getNext(p, _next):
_next[0] = -1
i = 0
j = -1
while i < len(p) - 1:
if j == -1 or p[i] == p[j]:
i += 1
j += 1
_next[i] = j
else:
j = _next[j]
getNext(p, _next)
i = 0
j = 0
while i < len(t) and j < len(p):
if j == -1 or t[i] == p[j]:
i += 1
j += 1
else:
j = _next[j]
if j == len(p):
return i - j
return -1
java
class Solution
{
public int strStr(String S, String T)
{
if (T == null || T.length() == 0)
return 0;
int[] next = new int[T.length()];
getNext(T, next);
int i = 0;
int j = 0;
while (i < S.length() && j < T.length())
{
if (j == -1 || S.charAt(i) == T.charAt(j))
{
i++;
j++;
}
else
j = next[j];
}
if (j == T.length()) return i - j;
return -1;
}
private void getNext(String t, int[] next)
{
next[0] = -1;
int i = 0;
int j = -1;
while (i < t.length() - 1)
{
if (j == -1 || t.charAt(i) == t.charAt(j))
{
i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
}
}
}