-
Notifications
You must be signed in to change notification settings - Fork 374
/
strstr_test.go
73 lines (61 loc) · 1.6 KB
/
strstr_test.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Problem:
- Implement strstr() that finds the first occurrence of the substring
needle in the string haystack. It returns -1 if needle is not part of the
haystack.
Example:
- Input: haystack = "aaabacd", needle = "ba"
Output: 3, because needle "ba" starts at index 3 in the haystack.
Approach:
- Scan the needle with the haystack from its first position and start matching
all subsequent letters one by one.
- If one letter does not match, start again with the next position in the
haystack.
Cost:
- O(nm) time, O(1) space, where n is the length of haystack while m is the
length of needle.
*/
package leetcode
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestStrstr(t *testing.T) {
tests := []struct {
in1 string
in2 string
expected int
}{
{"", "", -1},
{"", "a", -1},
{"dani", "a", 1},
{"danidani", "a", 1},
{"xxxdani", "dani", 3},
{"xxxdanixxx", "dani", 3},
}
for _, tt := range tests {
result := strstr(tt.in1, tt.in2)
common.Equal(t, tt.expected, result)
}
}
func strstr(haystack string, needle string) int {
for i := 0; i < len(haystack); i++ {
for j := 0; j < len(haystack); j++ {
// if the needle matches the haystack, meaning the index pointer
// reaches the end of the needle, return the index pointer.
if j == len(needle) {
return i
}
// return -1 if needle's length is greater than haystack's.
if i+j == len(haystack) {
return -1
}
// start again if one of the letter in the needles does not match
// one in the haystack.
if []rune(needle)[j] != []rune(haystack)[i+j] {
break
}
}
}
return -1
}