Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1973 from Ankush11903/patch-1
Browse files Browse the repository at this point in the history
Create 0028 - Find The Index of The First Occurrence In a String.CPP
  • Loading branch information
tahsintunan authored Jan 11, 2023
2 parents afb94e9 + d1d11af commit 5f198c8
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 5f198c8

Please sign in to comment.