Commit b3e8f5d
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
File tree
19 files changed
+530
-0
lines changed- sol/solution/0001-0100/0028
19 files changed
+530
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
0 commit comments