Leetcode 28. Find the Index of the First Occurrence in a String
Given two strings needle
and haystack
, return the index of the first occurrence of needle
in haystack
, or -1
if needle
is not part of haystack
.
haystack = "sadbutsad", needle = "sad"
Output: 0
haystack = "leetcode", needle = "leeto"
-1
needle
is an empty string?
needle
is an empty string, return 0
because it’s always found at the start.needle
is larger than haystack
?
-1
, since the needle
cannot be found inside a smaller haystack
.To solve this problem, we need to employ the classic string search algorithm. One of the simplest approaches is to use a sliding window technique to iterate over haystack
.
n
for the length of haystack
.m
for the length of needle
.needle
is empty, return 0
.needle
is longer than haystack
, return -1
.haystack
such that the window size is always equal to the length of needle
.haystack
with needle
.-1
if no match is found by the end of iterations.#include <iostream>
#include <string>
int strStr(const std::string& haystack, const std::string& needle) {
int n = haystack.length();
int m = needle.length();
if (m == 0) {
return 0; // Needle is an empty string.
}
if (m > n) {
return -1; // Needle is larger than haystack.
}
for (int i = 0; i <= n - m; i++) {
if (haystack.substr(i, m) == needle) {
return i; // Found the needle at index i.
}
}
return -1; // Needle not found in haystack.
}
int main() {
std::string haystack = "hello";
std::string needle = "ll";
std::cout << strStr(haystack, needle) << std::endl; // Output: 2
haystack = "aaaaa";
needle = "bba";
std::cout << strStr(haystack, needle) << std::endl; // Output: -1
return 0;
}
n
is the length of haystack
and m
is the length of needle
. This is due to the substring comparison at each possible starting point.
n - m + 1
starting points, and substring comparison at each point takes O(m) time.This solution works well for small to moderately sized input strings but may not be efficient for very large strings. For large inputs, more advanced string search algorithms like KMP (Knuth-Morris-Pratt) could be used to improve performance.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?