-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28. Implement strStr().c
256 lines (215 loc) · 4.73 KB
/
28. Implement strStr().c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
alphabet_number function 用來計算總共的字數
依序循序走訪字串
遇到相同的開始判斷並走訪
當 needle 走完代表找到相同的部分並回傳起始住標
如果中斷 haystack 退回原位後 +1 // 代表從下一個位置開始
index 用來判斷 needle 起始位置
沒找到相同的回傳 -1
*/
int strStr(char * haystack, char * needle){
char *pc = needle;
int index = 0, i = 0;
int input1 = alphabet_number(haystack), input2 = alphabet_number(needle);
if (!*needle && !*haystack)
return 0;
if (input1 < input2)
return -1;
else if (input1 && !input2)
return 0;
/*
while (*haystack) {
if (*pc == *haystack) {
index = i;
pc = needle + 1;
haystack++;
while (*pc) {
if (*pc == *haystack) {
pc++;
haystack++;
}else {
index = -1;
break;
}
}
if (index > 0)
return index;
}
haystack++;
i++;
}
*/
while (*haystack) {
if (*haystack == *pc) {
while ((*haystack == *pc) && *haystack != NULL && *pc != NULL) {
i++;
haystack++;
pc++;
}
if (*pc == NULL) {
return index;
}else {
pc = needle;
haystack = haystack - i + 1;
i = 0;
index++;
}
}else {
haystack++;
index++;
}
}
return -1;
}
int alphabet_number(char *pc)
{
int i = 0;
while (*pc) {
i++;
pc++;
}
return i;
}
/** C faster
int strStr(char * haystack, char * needle){
int len_haystack = strlen(haystack);
int len_needle = strlen(needle);
int i = 0, j= 0;
int temp = 0;
for(i=0; i<=(len_haystack-len_needle); i++)
{
temp = i;
for(j=0;j<len_needle;j++)
{
if(haystack[temp] == needle[j]){}
else break;
temp++;
}
if(j == len_needle) return i;
}
return -1;
}
*/
/** C
int strStr(char* haystack, char* needle) {
if(needle == NULL || *needle == '\0') return 0;
int i, imax;
int lhaystack = strlen(haystack);
int lneedle = strlen(needle);
imax = lhaystack - lneedle + 1;
for(i = 0; i < imax; i++) {
if(!strncmp(haystack + i, needle, lneedle)) return i;
}
return -1;
}
/** C
void GetNext(char* P, int len, int* next)
{
if(NULL == P)
return;
next[0] = -1;
int k=-1;
int j=0;
while(j < len-1)
{
if(-1 == k || P[k] == P[j])
{
++k;
++j;
next[j] = k;
}
else
k = next[k];
}
}
int strStr(char * haystack, char * needle){
if(NULL == haystack)
return -1;
if(NULL == needle)
return 0;
int Plen = strlen(needle);
if(0 == Plen)
return 0;
int *next = NULL;
next = (int*)calloc(Plen,sizeof(int));
if(NULL == next)
return -1;
int i = 0;
int j = 0;
int Tlen = strlen(haystack);
GetNext(needle, Plen, next);
while (haystack[i] != '\0' && j < Plen)
{
if (j == -1 || haystack[i] == needle[j])
{
++i;
++j;
}
else
{
j = next[j];
}
}
if(next)
free(next);
if (j == Plen)
return i - j;
else
return -1;
}
*/
/** Java
class Solution {
public int strStr(String haystack, String needle) {
if(needle.isEmpty()){
return 0;
}
if(needle.length() > haystack.length()) {
return -1;
}
if(needle.equals(haystack)) {
return 0;
}
int nLen = needle.length();
int boundary = haystack.length()-nLen;
for(int i=0; i<=boundary; i++) {
String sub = haystack.substring(i, nLen+i);
if(sub.equals(needle)) {
return i;
}
}
return -1;
}
}
public class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
*/
/** C++
class Solution {
public:
int strStr(string haystack, string needle) {
// check if str is empty
// search haystack
// return anwer
// -
if (needle.length() == 0){return 0;}
auto index = haystack.find(needle, 0);
if (index != string::npos){
return index;
}
else {return -1;}
}
};
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.length()<needle.length())
return -1;
else
return haystack.find(needle);
}
};
*/