Skip to content

Commit 378a435

Browse files
committed
Add solution 28.
1 parent b1f425a commit 378a435

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $haystack
5+
* @param String $needle
6+
* @return Integer
7+
*/
8+
function strStr($haystack, $needle) {
9+
if(strlen($needle) == 0) return 0;
10+
if(strlen($haystack) == 0) return -1;
11+
if(strlen($haystack) == 1 && strlen($needle) == 1){
12+
if($haystack[0] == $needle[0]){
13+
return 0;
14+
}
15+
}
16+
17+
for($i = 0; $i < strlen($haystack) - strlen($needle) + 1; $i++){
18+
if($needle === substr($haystack, $i, strlen($needle))){
19+
return $i;
20+
}
21+
}
22+
return -1;
23+
}
24+
}

0 commit comments

Comments
 (0)