Skip to content

Commit b3e8f5d

Browse files
committed
28. Find the Index of the First Occurrence in a String
```Solution.c int strStr(char * haystack, char * needle){ int n = strlen(haystack), m = strlen(needle); if (m == 0) return 0; int lps[m]; lps[0] = 0; for (int i = 1, len = 0; i < m; ) { if (needle[i] == needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (int i = 0, j = 0; i < n; ) { if (haystack[i] == needle[j]) { i++; j++; if (j == m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } ``` ```Solution.cpp class Solution { public: int strStr(string haystack, string needle) { int n = haystack.size(), m = needle.size(); if (m == 0) return 0; vector<int> lps(m, 0); for (int i = 1, len = 0; i < m; ) { if (needle[i] == needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (int i = 0, j = 0; i < n; ) { if (haystack[i] == needle[j]) { i++; j++; if (j == m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } }; ``` ```Solution.cs public class Solution { public int StrStr(string haystack, string needle) { int n = haystack.Length, m = needle.Length; if (m == 0) return 0; int[] lps = new int[m]; for (int i = 1, len = 0; i < m; ) { if (needle[i] == needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (int i = 0, j = 0; i < n; ) { if (haystack[i] == needle[j]) { i++; j++; if (j == m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } } ``` ```Solution.dart class Solution { int strStr(String haystack, String needle) { int n = haystack.length, m = needle.length; if (m == 0) return 0; List<int> lps = List.filled(m, 0); for (int i = 1, len = 0; i < m;) { if (needle[i] == needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (int i = 0, j = 0; i < n;) { if (haystack[i] == needle[j]) { i++; j++; if (j == m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } } ``` ```Solution.erl -spec str_str(Haystack :: unicode:unicode_binary(), Needle :: unicode:unicode_binary()) -> integer(). str_str(Haystack, Needle) -> case Needle of <<>> -> 0; _ -> case binary:match(Haystack, Needle) of nomatch -> -1; {Pos, _Len} -> Pos end end. ``` ```Solution.ex defmodule Solution do @SPEC str_str(haystack :: String.t(), needle :: String.t()) :: integer def str_str(haystack, needle) do cond do needle == "" -> 0 true -> case :binary.match(haystack, needle) do :nomatch -> -1 {pos, _len} -> pos end end end end ``` ```Solution.go func strStr(haystack string, needle string) int { n, m := len(haystack), len(needle) if m == 0 { return 0 } lps := make([]int, m) for i, length := 1, 0; i < m; { if needle[i] == needle[length] { length++ lps[i] = length i++ } else if length > 0 { length = lps[length-1] } else { lps[i] = 0 i++ } } for i, j := 0, 0; i < n; { if haystack[i] == needle[j] { i++; j++ if j == m { return i - m } } else if j > 0 { j = lps[j-1] } else { i++ } } return -1 } ``` ```Solution.java class Solution { public int strStr(String haystack, String needle) { int n = haystack.length(), m = needle.length(); if (m == 0) return 0; int[] lps = new int[m]; for (int i = 1, len = 0; i < m; ) { if (needle.charAt(i) == needle.charAt(len)) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (int i = 0, j = 0; i < n; ) { if (haystack.charAt(i) == needle.charAt(j)) { i++; j++; if (j == m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } } ``` ```Solution.js /** * @param {string} haystack * @param {string} needle * @return {number} */ var strStr = function(haystack, needle) { const n = haystack.length, m = needle.length; if (m === 0) return 0; const lps = new Array(m).fill(0); for (let i = 1, len = 0; i < m;) { if (needle[i] === needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (let i = 0, j = 0; i < n;) { if (haystack[i] === needle[j]) { i++; j++; if (j === m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; }; ``` ```Solution.kt class Solution { fun strStr(haystack: String, needle: String): Int { val n = haystack.length val m = needle.length if (m == 0) return 0 val lps = IntArray(m) var len = 0 var i = 1 while (i < m) { if (needle[i] == needle[len]) { len++ lps[i] = len i++ } else if (len > 0) { len = lps[len - 1] } else { lps[i] = 0 i++ } } i = 0; var j = 0 while (i < n) { if (haystack[i] == needle[j]) { i++; j++ if (j == m) return i - m } else if (j > 0) { j = lps[j - 1] } else { i++ } } return -1 } } ``` ```Solution.php class Solution { /** * @param String $haystack * @param String $needle * @return Integer */ function strStr($haystack, $needle) { $n = strlen($haystack); $m = strlen($needle); if ($m == 0) return 0; $lps = array_fill(0, $m, 0); for ($i = 1, $len = 0; $i < $m; ) { if ($needle[$i] == $needle[$len]) { $lps[$i++] = ++$len; } elseif ($len > 0) { $len = $lps[$len - 1]; } else { $lps[$i++] = 0; } } for ($i = 0, $j = 0; $i < $n; ) { if ($haystack[$i] == $needle[$j]) { $i++; $j++; if ($j == $m) return $i - $m; } elseif ($j > 0) { $j = $lps[$j - 1]; } else { $i++; } } return -1; } } ``` ```Solution.py class Solution: def strStr(self, haystack: str, needle: str) -> int: n, m = len(haystack), len(needle) if m == 0: return 0 lps = [0] * m length = 0 i = 1 while i < m: if needle[i] == needle[length]: length += 1 lps[i] = length i += 1 elif length > 0: length = lps[length - 1] else: lps[i] = 0 i += 1 i = j = 0 while i < n: if haystack[i] == needle[j]: i += 1 j += 1 if j == m: return i - m elif j > 0: j = lps[j - 1] else: i += 1 return -1 ``` ```Solution.rb # @param {String} haystack # @param {String} needle # @return {Integer} def str_str(haystack, needle) n, m = haystack.length, needle.length return 0 if m == 0 lps = Array.new(m, 0) len = 0; i = 1 while i < m if needle[i] == needle[len] len += 1 lps[i] = len i += 1 elsif len > 0 len = lps[len - 1] else lps[i] = 0 i += 1 end end i = j = 0 while i < n if haystack[i] == needle[j] i += 1; j += 1 return i - m if j == m elsif j > 0 j = lps[j - 1] else i += 1 end end -1 end ``` ```Solution.rkt (define/contract (str-str haystack needle) (-> string? string? exact-integer?) (let* ((n (string-length haystack)) (m (string-length needle))) (cond [(= m 0) 0] [else (define lps (make-vector m 0)) (let loop ((i 1) (len 0)) (when (< i m) (cond [(char=? (string-ref needle i) (string-ref needle len)) (set! len (add1 len)) (vector-set! lps i len) (loop (add1 i) len)] [(> len 0) (loop i (vector-ref lps (sub1 len)))] [else (vector-set! lps i 0) (loop (add1 i) len)]))) (let search ((i 0) (j 0)) (cond [(= i n) -1] [(char=? (string-ref haystack i) (string-ref needle j)) (if (= (add1 j) m) (- (add1 i) m) (search (add1 i) (add1 j)))] [(> j 0) (search i (vector-ref lps (sub1 j)))] [else (search (add1 i) j)]))]))) ``` ```Solution.rs impl Solution { pub fn str_str(haystack: String, needle: String) -> i32 { let n = haystack.len(); let m = needle.len(); if m == 0 { return 0; } let h: Vec<char> = haystack.chars().collect(); let nd: Vec<char> = needle.chars().collect(); let mut lps = vec![0; m]; let (mut len, mut i) = (0, 1); while i < m { if nd[i] == nd[len] { len += 1; lps[i] = len; i += 1; } else if len > 0 { len = lps[len - 1]; } else { lps[i] = 0; i += 1; } } let (mut j, mut idx) = (0, 0); while idx < n { if h[idx] == nd[j] { idx += 1; j += 1; if j == m { return (idx - m) as i32; } } else if j > 0 { j = lps[j - 1]; } else { idx += 1; } } -1 } } ``` ```Solution.scala object Solution { def strStr(haystack: String, needle: String): Int = { val n = haystack.length val m = needle.length if (m == 0) return 0 val lps = Array.fill(m)(0) var len = 0 var i = 1 while (i < m) { if (needle(i) == needle(len)) { len += 1 lps(i) = len i += 1 } else if (len > 0) { len = lps(len - 1) } else { lps(i) = 0 i += 1 } } i = 0; var j = 0 while (i < n) { if (haystack(i) == needle(j)) { i += 1; j += 1 if (j == m) return i - m } else if (j > 0) { j = lps(j - 1) } else { i += 1 } } -1 } } ``` ```Solution.swift class Solution { func strStr(_ haystack: String, _ needle: String) -> Int { let n = haystack.count, m = needle.count if m == 0 { return 0 } let h = Array(haystack), nd = Array(needle) var lps = [Int](repeating: 0, count: m) var len = 0, i = 1 while i < m { if nd[i] == nd[len] { len += 1 lps[i] = len i += 1 } else if len > 0 { len = lps[len - 1] } else { lps[i] = 0 i += 1 } } i = 0; var j = 0 while i < n { if h[i] == nd[j] { i += 1; j += 1 if j == m { return i - m } } else if j > 0 { j = lps[j - 1] } else { i += 1 } } return -1 } } ``` ```Solution.ts function strStr(haystack: string, needle: string): number { const n = haystack.length, m = needle.length; if (m === 0) return 0; const lps: number[] = new Array(m).fill(0); for (let i = 1, len = 0; i < m;) { if (needle[i] === needle[len]) { lps[i++] = ++len; } else if (len > 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } for (let i = 0, j = 0; i < n;) { if (haystack[i] === needle[j]) { i++; j++; if (j === m) return i - m; } else if (j > 0) { j = lps[j - 1]; } else { i++; } } return -1; } ```
1 parent bd1ba5a commit b3e8f5d

19 files changed

+530
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# [**28. Find the Index of the First Occurrence in a String**](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/)
2+
3+
Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or `-1` if `needle` is not part of `haystack`.
4+
5+
#### **Example 1:**
6+
```MD
7+
Input: haystack = "sadbutsad", needle = "sad"
8+
Output: 0
9+
Explanation: "sad" occurs at index 0 and 6.
10+
The first occurrence is at index 0, so we return 0.
11+
```
12+
13+
#### **Example 2:**
14+
```md
15+
Input: haystack = "leetcode", needle = "leeto"
16+
Output: -1
17+
Explanation: "leeto" did not occur in "leetcode", so we return -1.
18+
```
19+
20+
#### **Constraints:**
21+
> - `1 <= haystack.length, needle.length <= 104`
22+
> - `haystack` and `needle` consist of only lowercase English characters.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int strStr(char * haystack, char * needle){
2+
int n = strlen(haystack), m = strlen(needle);
3+
if (m == 0) return 0;
4+
int lps[m];
5+
lps[0] = 0;
6+
for (int i = 1, len = 0; i < m; ) {
7+
if (needle[i] == needle[len]) {
8+
lps[i++] = ++len;
9+
} else if (len > 0) {
10+
len = lps[len - 1];
11+
} else {
12+
lps[i++] = 0;
13+
}
14+
}
15+
for (int i = 0, j = 0; i < n; ) {
16+
if (haystack[i] == needle[j]) {
17+
i++; j++;
18+
if (j == m) return i - m;
19+
} else if (j > 0) {
20+
j = lps[j - 1];
21+
} else {
22+
i++;
23+
}
24+
}
25+
return -1;
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int strStr(string haystack, string needle) {
4+
int n = haystack.size(), m = needle.size();
5+
if (m == 0) return 0;
6+
vector<int> lps(m, 0);
7+
for (int i = 1, len = 0; i < m; ) {
8+
if (needle[i] == needle[len]) {
9+
lps[i++] = ++len;
10+
} else if (len > 0) {
11+
len = lps[len - 1];
12+
} else {
13+
lps[i++] = 0;
14+
}
15+
}
16+
for (int i = 0, j = 0; i < n; ) {
17+
if (haystack[i] == needle[j]) {
18+
i++; j++;
19+
if (j == m) return i - m;
20+
} else if (j > 0) {
21+
j = lps[j - 1];
22+
} else {
23+
i++;
24+
}
25+
}
26+
return -1;
27+
}
28+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public int StrStr(string haystack, string needle) {
3+
int n = haystack.Length, m = needle.Length;
4+
if (m == 0) return 0;
5+
int[] lps = new int[m];
6+
for (int i = 1, len = 0; i < m; ) {
7+
if (needle[i] == needle[len]) {
8+
lps[i++] = ++len;
9+
} else if (len > 0) {
10+
len = lps[len - 1];
11+
} else {
12+
lps[i++] = 0;
13+
}
14+
}
15+
for (int i = 0, j = 0; i < n; ) {
16+
if (haystack[i] == needle[j]) {
17+
i++; j++;
18+
if (j == m) return i - m;
19+
} else if (j > 0) {
20+
j = lps[j - 1];
21+
} else {
22+
i++;
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
int strStr(String haystack, String needle) {
3+
int n = haystack.length, m = needle.length;
4+
if (m == 0) return 0;
5+
List<int> lps = List.filled(m, 0);
6+
for (int i = 1, len = 0; i < m;) {
7+
if (needle[i] == needle[len]) {
8+
lps[i++] = ++len;
9+
} else if (len > 0) {
10+
len = lps[len - 1];
11+
} else {
12+
lps[i++] = 0;
13+
}
14+
}
15+
for (int i = 0, j = 0; i < n;) {
16+
if (haystack[i] == needle[j]) {
17+
i++; j++;
18+
if (j == m) return i - m;
19+
} else if (j > 0) {
20+
j = lps[j - 1];
21+
} else {
22+
i++;
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-spec str_str(Haystack :: unicode:unicode_binary(),
2+
Needle :: unicode:unicode_binary()) -> integer().
3+
str_str(Haystack, Needle) ->
4+
case Needle of
5+
<<>> -> 0;
6+
_ ->
7+
case binary:match(Haystack, Needle) of
8+
nomatch -> -1;
9+
{Pos, _Len} -> Pos
10+
end
11+
end.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule Solution do
2+
@spec str_str(haystack :: String.t(), needle :: String.t()) :: integer
3+
def str_str(haystack, needle) do
4+
cond do
5+
needle == "" ->
6+
0
7+
true ->
8+
case :binary.match(haystack, needle) do
9+
:nomatch -> -1
10+
{pos, _len} -> pos
11+
end
12+
end
13+
end
14+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func strStr(haystack string, needle string) int {
2+
n, m := len(haystack), len(needle)
3+
if m == 0 {
4+
return 0
5+
}
6+
lps := make([]int, m)
7+
for i, length := 1, 0; i < m; {
8+
if needle[i] == needle[length] {
9+
length++
10+
lps[i] = length
11+
i++
12+
} else if length > 0 {
13+
length = lps[length-1]
14+
} else {
15+
lps[i] = 0
16+
i++
17+
}
18+
}
19+
for i, j := 0, 0; i < n; {
20+
if haystack[i] == needle[j] {
21+
i++; j++
22+
if j == m {
23+
return i - m
24+
}
25+
} else if j > 0 {
26+
j = lps[j-1]
27+
} else {
28+
i++
29+
}
30+
}
31+
return -1
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
int n = haystack.length(), m = needle.length();
4+
if (m == 0) return 0;
5+
int[] lps = new int[m];
6+
for (int i = 1, len = 0; i < m; ) {
7+
if (needle.charAt(i) == needle.charAt(len)) {
8+
lps[i++] = ++len;
9+
} else if (len > 0) {
10+
len = lps[len - 1];
11+
} else {
12+
lps[i++] = 0;
13+
}
14+
}
15+
for (int i = 0, j = 0; i < n; ) {
16+
if (haystack.charAt(i) == needle.charAt(j)) {
17+
i++; j++;
18+
if (j == m) return i - m;
19+
} else if (j > 0) {
20+
j = lps[j - 1];
21+
} else {
22+
i++;
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function(haystack, needle) {
7+
const n = haystack.length, m = needle.length;
8+
if (m === 0) return 0;
9+
const lps = new Array(m).fill(0);
10+
for (let i = 1, len = 0; i < m;) {
11+
if (needle[i] === needle[len]) {
12+
lps[i++] = ++len;
13+
} else if (len > 0) {
14+
len = lps[len - 1];
15+
} else {
16+
lps[i++] = 0;
17+
}
18+
}
19+
for (let i = 0, j = 0; i < n;) {
20+
if (haystack[i] === needle[j]) {
21+
i++; j++;
22+
if (j === m) return i - m;
23+
} else if (j > 0) {
24+
j = lps[j - 1];
25+
} else {
26+
i++;
27+
}
28+
}
29+
return -1;
30+
};

0 commit comments

Comments
 (0)