Skip to content

Commit

Permalink
Solve: Find the Index of the First Occurrence in a String
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Oct 4, 2024
1 parent a7ec631 commit be7ca6b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LeetCode/Find the Index of the First Occurrence in a String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using namespace std;

class Solution {
public:
int strStr(string haystack, string needle) {
int haystackLen = haystack.length();
int needleLen = needle.length();

if (needleLen == 0) return 0;

for (int i = 0; i <= haystackLen - needleLen; i++){
if (haystack.substr(i, needleLen) == needle){
return i;
}
}

return -1;
}
};

0 comments on commit be7ca6b

Please sign in to comment.