๐ŸŒ
GitHub
github.com โ€บ doocs โ€บ leetcode โ€บ blob โ€บ main โ€บ solution โ€บ 0000-0099 โ€บ 0028.Find the Index of the First Occurrence in a String โ€บ README_EN.md
leetcode/solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README_EN.md at main ยท doocs/leetcode
Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1. ... We compare the string needle with each character of the string haystack as the starting point.
Author ย  doocs
๐ŸŒ
Codeewander
codeewander.github.io โ€บ leetcode 28 - implement strstr()
LeetCode 28 - Implement strStr() | Kira Yang
Skip to main content ยท On this page ยท Difficulty: Easy. Related Topics: Two Pointers, String. Link: leetcode ยท Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" ...
๐ŸŒ
Red Quark
redquark.org โ€บ leetcode โ€บ 0028-implement-strstr
LeetCode #28 - Implement StrStr | Red Quark
Hello fellow devs ๐Ÿ‘‹! We have a new LeetCode problem today involving string. ... Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. What should we return when needle is an empty string? This is a great question to ask during an interview.
๐ŸŒ
DEV Community
dev.to โ€บ grantdotdev โ€บ c-leetcode-28-find-the-index-of-the-the-first-occurrence-in-a-string-4196
C# LeetCode 28: Find the Index of the the First Occurrence in a String - DEV Community
August 4, 2025 - Input: haystack = "leetcode", needle = "leeto" Output: -1 *Explanation: *"leeto" did not occur in "leetcode", so we return -1. Iteration 1: (Poor) - Tried without built in functions ยท public int StrStr(string haystack, string needle) { if (haystack.Length < needle.Length) return -1; for (var i = 0; i < haystack.Length; i++) { if (haystack[i..].StartsWith(needle)) return i; } return -1; } Well, there's unnecessary allocation of strings. haystack[i..] creates a new string on every iteration, which is costly for large strings.
๐ŸŒ
Medium
medium.com โ€บ @rutujakbhombe19 โ€บ leetcode-28-find-the-index-of-the-first-occurrence-in-a-string-03f36240f6bd
Leetcode 28: Find the Index of the First Occurrence in a String | by Rutuja Bhombe | Medium
August 11, 2024 - Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1. ... You have two strings: haystack (the string in which you're searching) and needle (the string you're searching for). Calculate the lengths of haystack and needle.
๐ŸŒ
LeetCode
leetcode.ca โ€บ all โ€บ 28.html
Leetcode 28. Implement strStr()
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ leetcode-28-implement-strstr-solution-with-images-7066d68c2262
LeetCode: 28. Implement strStr() (Solution with images) | by Alex Murphy | Dev Genius
August 2, 2022 - LeetCode: 28. Implement strStr() (Solution with images) Link: โ†’ https://leetcode.com/problems/implement-strstr/ Problem: โ†’ Implement strStr(). Given two strings needle and haystack, return the โ€ฆ
Find elsewhere
๐ŸŒ
AlgoMonster
algo.monster โ€บ liteproblems โ€บ 28
28. Find the Index of the First Occurrence in a String - In-Depth Explanation
In-depth solution and explanation for LeetCode 28. Find the Index of the First Occurrence in a String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
๐ŸŒ
Leetcode
leetcode.ca
Leetcode Solutions in Java Python C++ Php Go Typescript Swift C# Scala Ruby RenderScript SQL | Leetcode
Leetcode solutions, algorithm explaination, in Java Python C++ Php Go Typescript Javascript
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ find-the-index-of-the-first-occurrence-in-a-string
Find the Index of the First Occurrence in a String - LeetCode
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.
Top answer
1 of 2
3

The function has several drawbacks.

For starters it should be declared like

std::string::size_type strStr( const std::string &haystack, const std::string &needle );

and if the second string is not found in the first string the function should return std::string::npos as all similar member functions of the class std::string do.

The function parameters shell be of constant referenced types.

The condition in this if-statement

if (haystack.empty() && !needle.empty())

has a redundant operand. It could be rewritten like

if (haystack.empty())

This loop

for (i; i < haystack.length(); i++) 

should stop its iterations when the size of the tail of the first string is less than the size of the second string.

in this if-statement

if (haystack[i++] != needle[j]) {

the variable i is incremented that results in incrementing the variable two times: one in this statement and the second time in the loop.

The second pair of these statements

        if (j == needle.length()) {
        return ans;

is redundant.

The function can be written the following way as it is shown in the demonstrative program.

#include <iostream>
#include <string>

std::string::size_type strStr( const std::string &haystack, const std::string &needle )
{
    if ( needle.empty() )
    {
        return 0;
    }
    else if ( haystack.empty() )
    {
        return -std::string::npos;
    }
    else
    {
        std::string::size_type ans = std::string::npos;

        auto n1 = haystack.length();
        auto n2 = needle.length();

        for ( std::string::size_type i = 0; ans == std::string::npos && i + n2 <= n1; i++ )
        {
            std::string::size_type j = 0;
            while ( j < n2 && haystack[i+j] == needle[j] ) j++;

            if ( j == n2 ) ans = i;
        }

        return ans;
    }
}

int main() 
{
    std::string haystack( "mississippi" );
    std::string needle( "issip" );

    std::cout << strStr( haystack, needle ) << '\n';

    return 0;
}

Its output is

4

2 of 2
2

The problem is that you modify i in

if (haystack[i++] != needle[j]) {

Thus preventing a second potential match from being explored. Try

if (haystack[i + j] != needle[j]) {

and fix any knock-on issues. I expect it to work as-is, though.

๐ŸŒ
Eugenejw
eugenejw.github.io โ€บ 2017 โ€บ 07 โ€บ leetcode-28
Weihan's Coding Blog | Implement strStr method (leetcode 28)
public class Solution { /** * Return the starting index of the first needle found in haystack * @param String haystack of type String * @param String needle of type String * @return int the starting index */ public int strStr(String haystack, String needle) { int[] kmpTable= new int[needle.length()]; buildKMPTable(needle, kmpTable); int i = 0; int j = 0; int N = haystack.length(); int M = needle.length(); while (i < N && j < M) { if (haystack.charAt(i) == needle.charAt(j)) { i++; if (++j == M) { return i - j; } } else { if (j == 0) { i++; } else{ j = kmpTable[j-1]; } } } if (M == 0) return 0;
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ move-zeroes
Move Zeroes - LeetCode
Move Zeroes - Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.
๐ŸŒ
LeetCode
leetcode.com โ€บ problemset
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ total-appeal-of-a-string
Total Appeal of A String - LeetCode
Can you solve this real interview question? Total Appeal of A String - The appeal of a string is the number of distinct characters found in the string. * For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72795129 โ€บ leetcode-28-implement-strstr-why-my-code-failed
debugging - Leetcode 28. Implement strStr() -- Why my code failed? - Stack Overflow
My code failed on haystack = "hello", needle = "ll". Expected: 2, my output: -1. Here's my code: class Solution { public int strStr(String haystack, String needle) { if (nee...
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ letter-combinations-of-a-phone-number
Letter Combinations of a Phone Number - LeetCode
Can you solve this real interview question? Letter Combinations of a Phone Number - Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.