Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int strStr(string haystack, string needle) {
- int i=0;
- int pos;
- bool f=true;
- if(haystack.length()<needle.length())
- {
- return -1;
- }
- while(i<=haystack.length()-needle.length())
- {
- f=true;
- int j=i+needle.length()-1;
- if(haystack[i]==needle[0] && haystack[j]==needle[needle.length()-1])
- {
- int a=i+1;
- int k=1;
- while(a<=j-1)
- {
- if(haystack[a]!=needle[k])
- {
- f=false;
- break;
- }
- a++;
- k++;
- }
- if(f)
- {
- pos=i;
- f=true;
- break;
- }
- }
- f=false;
- i++;
- }
- if(f)
- {
- return pos;
- }
- else
- {
- return -1;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment