Find the Index of the First Occurrence in a String Problem
Find the Index of the First Occurrence in a String Problem — ExecCode Easy DSA Practice
Solve the Find the Index of the First Occurrence in a String problem on ExecCode. Free online easy DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.
Problem description
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.
Examples
Input haystack = "sadbutsad", needle = "sad"; Output 0. Input haystack = "leetcode", needle = "leeto"; Output -1
Constraints
1 ≤ haystack.length, needle.length ≤ 10⁴ haystack and needle consist of only lowercase English characters
Practice Find the Index of the First Occurrence in a String free on ExecCode. Browse DSA problems, topic map, and placement guides.