Skip to content

Commit 5f198c8

Browse files
authored
Merge pull request neetcode-gh#1973 from Ankush11903/patch-1
Create 0028 - Find The Index of The First Occurrence In a String.CPP
2 parents afb94e9 + d1d11af commit 5f198c8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int strStr(string haystack, string needle) {
4+
if(haystack.size()<needle.size()) return -1;
5+
int found=0;
6+
for(int i=0;i<haystack.size()-needle.size()+1;i++){
7+
if(haystack[i]==needle[0]){
8+
found=1;
9+
for(int j=1;j<needle.size();j++){
10+
if(haystack[i+j]!=needle[j]){
11+
found=0;break;
12+
}
13+
}if(found==1) return i;
14+
}
15+
}return -1;
16+
}
17+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy