1 #include<iostream>
2 #include<string>
3 using namespace std;
4 class Solution {
5 public:
6 int strStr(string haystack, string needle) {
7 if (haystack == "") {
8 return 0;
9 }
10 int sign = 0;
11 for (int i = 0; i < haystack.size() - needle.size() + 1&& haystack.size()>= needle.size(); i++) {
12 if (haystack[i] == needle[0]) {
13 sign = i;
14 for (int j = 0; j < needle.size();j++) {
15 if (haystack[sign] == needle[j]) {
16 sign = sign + 1;
17 }
18 else
19 {
20 break;
21 }
22 }
23 if (sign - i == needle.size()) {
24 return i;
25 }
26 }
27 }
28 return -1;
29 }
30 };
31 int main() {
32 string haystack = "abb";
33 string needle = "bb";
34 Solution sol;
35 int num=sol.strStr(haystack, needle);
36 cout << num << endl;
37
38 }