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.
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
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. ... Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. ... Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1.
Author doocs
Medium
medium.com › @Neelesh-Janga › q-28-leetcode-find-the-index-of-the-first-occurrence-in-a-string-using-java-c56628b2d33c
Q-28 LeetCode: Find the Index of the First Occurrence in a String using Java
December 9, 2023 - Time Complexity: O( n * m ) - Where n is the length of haystack and m is the length of needle - Runtime: 0 ms (Beats 100% of Java submissions in LeetCode) - Note: The mentioned time complexity is for worst case scenario. Space Complexity: O( 1 ) - Memory Consumed: 40.5 MB · class Solution { public int strStr(String haystack, String needle) { int hayEnd = haystack.length(); int needEnd = needle.length(); for(int i = 0; i < hayEnd; i++){ while (i < hayEnd && haystack.charAt(i) != needle.charAt(0)) i++; if(i < hayEnd){ int start = i + 1; int needIndex = 1; while(start < hayEnd && needIndex < needEnd && haystack.charAt(start) == needle.charAt(needIndex)){ start++; needIndex++; } if (needIndex == needEnd) return i; } } return -1; } }
WalkCCC
walkccc.me › LeetCode › problems › 28
28. Find the Index of the First Occurrence in a String - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
Medium
medium.com › @subhanamjad507 › leetcode-28-find-the-index-of-the-first-occurrence-in-a-string-bf8e15260775
LeetCode 28. Find the Index of the First Occurrence in a String | by Subhan Amjad | Medium
August 29, 2024 - class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; // If needle is empty, return 0 int n = haystack.length(); int m = needle.length(); for (int i = 0; i <= n - m; i++) { int j = 0; // Check if the substring of haystack starting from i matches needle while (j < m && haystack[i + j] == needle[j]) { j++; } if (j == m) { return i; // Found the first occurrence of needle } } return -1; } }; I tackle LeetCode problems daily and share detailed solutions and approaches on my blog.
Reddit
reddit.com › r/leetcode › can this be done this way ? find the index of the first occurrence in a string
r/leetcode on Reddit: Can this be done this way ? Find the Index of the First Occurrence in a String
December 22, 2023 -
public int strStr(String haystack, String needle) {
for(int i=0;i<haystack.length()-needle.length()+1;i++)
{
if(haystack.substring(i,i+needle.length()).equals(needle))
{
return i;
}
}
return -1;
}Everywhere i see they have done with different way I managed to come up with this solution. So is this accepted or do i need to do the way shown in youtube videos?
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.
DEV Community
dev.to › rahulgithubweb › leetcode-challenge-28-find-the-index-of-the-first-occurrence-in-a-string-javascript-solution-3lfb
LeetCode Challenge: 28. Find the Index of the First Occurrence in a String - JavaScript Solution 🚀 - DEV Community
December 26, 2024 - Let’s break it down Find the Index of the First Occurrence in a String and solve it using multiple approaches, including a manual implementation. ... Return the index of the first occurrence of needle in haystack. Return -1 if needle does not exist in haystack. ... Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" appears first at index 0. ... Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" is not part of "leetcode".
YouTube
youtube.com › neetcode
Find the Index of the First Occurrence in a String - Leetcode 28 - Python - YouTube
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews🥷 Discord: https://discord.gg/ddjKRXPqtk🐦 Twitter: https://twitter.com/neetcode1🐮 S...
Published November 10, 2021 Views 62K
Medium
medium.com › @MonlesYen › leetcode-python-c-c-find-the-index-of-the-first-occurrence-in-a-string-ba8e0821d562
Leetcode Python , C & C++ — Find the Index of the First Occurrence in a String | by Yen Wang | Medium
August 11, 2023 - 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. ... Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. ... Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1.
DEV Community
dev.to › debeshpg90 › 28-find-the-index-of-the-first-occurrence-in-a-string-leetcode-top-interview-150-coding-6c6
28. Find the Index of the First Occurrence in a String | LeetCode | Top Interview 150 | Coding Questions - DEV Community
December 28, 2025 - class Solution { public int strStr(String haystack, String needle) { int n1 = haystack.length(); int n2 = needle.length(); if (n2 == 0) { return -1; } for (int i = 0; i <= n1 - n2; i++) { if (haystack.charAt(i) == needle.charAt(0)) { if (compare(haystack, needle, i)) { return i; } } } return -1; } private boolean compare(String haystack, String needle, int idx) { int n1 = haystack.length(); int n2 = needle.length(); if (idx + n2 > n1) { return false; } for (int i = 0; i < n2; i++) { if (needle.charAt(i) != haystack.charAt(idx + i)) { return false; } } return true; } } ... #leetcode #programming #ai #beginners 151.
GitHub
github.com › LeetCode-Feedback › LeetCode-Feedback › issues › 24614
Missing Test Case - 28. Find the Index of the First Occurrence in a String · Issue #24614 · LeetCode-Feedback/LeetCode-Feedback
October 5, 2024 - import java.util.*; public class Solution { public int strStr(String haystack, String needle) { // If needle is empty, return 0 as per convention if (needle.isEmpty()) { return 0; } // Use indexOf to find the starting index of needle in haystack return haystack.indexOf(needle); } public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.print("Enter the substring that that you wanted to search in the main string:-\t"); String needle=sc.nextLine(); System.out.println("Enter the word among who the substrings existence is to be found:-\t"); String haystack=sc.nextLine(); int startIndex=haystack.indexOf(needle); if (startIndex!=-1){ System.out.println(startIndex); }else{ System.out.println(startIndex); } } }
Published Oct 05, 2024