diff --git a/LeetCode/Find the Index of the First Occurrence in a String.cpp b/LeetCode/Find the Index of the First Occurrence in a String.cpp new file mode 100644 index 0000000..899fc10 --- /dev/null +++ b/LeetCode/Find the Index of the First Occurrence in a String.cpp @@ -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; + } +}; \ No newline at end of file