Skip to content

Commit

Permalink
Create 0028 - Find The Index of The First Occurrence In a String.CPP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush11903 authored Jan 9, 2023
1 parent e7534ad commit 6b6fad9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cpp/0028 - Find The Index of The First Occurrence In a String.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.size()<needle.size()) return -1;
int found=0;
for(int i=0;i<haystack.size()-needle.size()+1;i++){
if(haystack[i]==needle[0]){
found=1;
for(int j=1;j<needle.size();j++){
if(haystack[i+j]!=needle[j]){
found=0;break;
}
}if(found==1) return i;
}
}return -1;
}
};

0 comments on commit 6b6fad9

Please sign in to comment.