/*
28. Implement strStr() My Submissions QuestionEditorial Solution
Total Accepted: 102448 Total Submissions: 413336 Difficulty: Easy
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
*/
/*
解题思路:
方法一:使用string自带的函数 find ,一句话搞定
方法二:逐一遍历,截取等长字符串进行比较
*/
class Solution {
public:
int strStr(string haystack, string needle) {
//使用STL,一句话搞定
// return haystack.find(needle);
int m=haystack.size(),n=needle.size();
if(m<n)return -1;
for(int i=0;i<m-n+1;i++){
if(haystack.substr(i,n)==needle)return i;
}
return -1;
}
};