-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimplement_strStr.dart
129 lines (107 loc) · 3.24 KB
/
implement_strStr.dart
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Implement strStr().
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.
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Constraints:
1 <= haystack.length, needle.length <= 104
haystack and needle consist of only lowercase English characters.
*/
void main() {
final a = E().strStr("aaa", "aaaa");
print(a);
}
class A {
// RangeError (index): Invalid value: Not in inclusive range 0..2: 3
int strStr(String haystack, String needle) {
if (needle.isEmpty && haystack.isEmpty) return 0;
for (var i = 0; i < haystack.length; i++) {
for (var j = 0; j < needle.length; j++) {
if (haystack[i + j] != needle[j]) break;
if (j == needle.length - 1) return i;
}
}
return -1;
}
}
class B {
//RangeError (index): Invalid value: Not in inclusive range 0..2: 3
int strStr(String haystack, String needle) {
int haystackLength = haystack.length;
int haystackLengthCopy = haystackLength;
int needleLength = needle.length;
int firstIndex = 0;
int needlePosition = 0;
bool boMatch = true;
if (needleLength == 0) return 0;
while (haystackLengthCopy != 0) {
for (var i = haystackLength - haystackLengthCopy;
i < (haystackLength - haystackLengthCopy) + needleLength;
i++) {
if (haystack[i] != needle[needlePosition++]) {
boMatch = false;
break;
} else {
boMatch = true;
}
}
if (boMatch == true) {
firstIndex = haystackLength - haystackLengthCopy;
break;
} else {
needlePosition = 0;
haystackLengthCopy -= 1;
}
if (boMatch == false) return -1;
}
return firstIndex;
}
}
class C {
// RangeError (index): Invalid value: Not in inclusive range 0..2: 3
int strStr(String haystack, String needle) {
// if (needle.length == 0) return 0;
int ans = -1;
int h = haystack.length;
int n = needle.length;
for (var i = 0; i < h; i++) {
if (haystack[i] == needle[0]) {
if (haystack.substring(i, n) == needle) {
ans = i;
break;
}
}
}
return ans;
}
}
class D {
int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
/*
Runtime: 457 ms, faster than 46.15% of Dart online submissions for Implement strStr().
Memory Usage: 140 MB, less than 84.62% of Dart online submissions for Implement strStr().
*/
}
class E {
// RangeError (end): Invalid value: Not in inclusive range 1..3: 4
int strStr(String haystack, String needle) {
int len = needle.length;
var first = haystack.substring(1, needle.length);
if (first == needle) return 0;
for (var i = 1; i < haystack.length - needle.length + 1; ++i) {
first = first.substring(1);
first += haystack[i + len - 1];
if (first == needle) return i;
}
return -1;
}
}