28. Find the Index of the First Occurrence in a String
Find the Index of the First Occurrence in a String (LeetCode 28) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Naive / KMP / Rabin-Karp subpattern.
How to solve Find the Index of the First Occurrence in a String
Find the Index of the First Occurrence in a String is a String Matching problem, part of String Manipulation Patterns. Master string matching algorithms including naive search, KMP pattern matching, and Rabin-Karp hashing.
Work towards the solution in these steps:
- Start by considering how you might check if one string appears inside another using simple iteration.
- Think about sliding a window over the haystack string that matches the length of the needle to compare substrings.
- Try to compare each substring of haystack with needle character by character and return the index if all characters match.
The reference solution runs in O(n + m) time and O(m) space.
The Find the Index of the First Occurrence in a String solution and editorial walks through the full approach with reference code.
Problem statement
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.
Example 1:
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.
Example 2:
Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1.
Constraints:
- 1 <= haystack.length, needle.length <= 10^4
- haystack and needle consist of only lowercase English characters.
Topics and companies
Topics: Two Pointers, String, String Matching.
How to practise Find the Index of the First Occurrence in a String on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n + m) time and O(m) space.
Open Find the Index of the First Occurrence in a String in the code editor, or read the Find the Index of the First Occurrence in a String editorial for a worked solution with its approach and complexity analysis.
Where Find the Index of the First Occurrence in a String sits in the DSA pattern sheet
- String Manipulation Patterns pattern guide
- Naive / KMP / Rabin-Karp subpattern
- The full DSA patterns sheet
Problems related to Find the Index of the First Occurrence in a String
Other problems that use the same Naive / KMP / Rabin-Karp and String Manipulation Patterns techniques:
- Find Beautiful Indices in the Given Array II — Hard, same subpattern
- Longest Common Prefix — Easy, same subpattern
- Repeated String Match — Medium, same subpattern
- Rotate String — Easy, same subpattern
- Shortest Palindrome — Hard, same subpattern
- Add Strings — Easy, same pattern
Find the Index of the First Occurrence in a String FAQ
What is the LeetCode number of Find the Index of the First Occurrence in a String?
Find the Index of the First Occurrence in a String is LeetCode problem 28, rated Easy.
What pattern is used to solve Find the Index of the First Occurrence in a String?
Find the Index of the First Occurrence in a String is a String Matching problem. It belongs to the String Manipulation Patterns pattern, in the Naive / KMP / Rabin-Karp subpattern.
What is the time complexity of Find the Index of the First Occurrence in a String?
The reference solution runs in O(n + m) time and O(m) space.