|
| 1 | +--- |
| 2 | +id: find-first-occurrence-in-string |
| 3 | +title: Find the Index of the First Occurrence in a String |
| 4 | +difficulty: Easy |
| 5 | +sidebar_label: 0028-FindFirstOccurrenceInString |
| 6 | +tags: |
| 7 | + - String |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | |
| 14 | +| [Remove Duplicates from Sorted Array](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/) | [Remove Duplicates from Sorted Array Solution on LeetCode](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +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`. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +#### Example 1: |
| 23 | + |
| 24 | +- **Input:** |
| 25 | + - `haystack = "sadbutsad"` |
| 26 | + - `needle = "sad"` |
| 27 | +- **Output:** `0` |
| 28 | +- **Explanation:** |
| 29 | + - "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. |
| 30 | + |
| 31 | +#### Example 2: |
| 32 | + |
| 33 | +- **Input:** |
| 34 | + - `haystack = "leetcode"` |
| 35 | + - `needle = "leeto"` |
| 36 | +- **Output:** `-1` |
| 37 | +- **Explanation:** |
| 38 | + - "leeto" did not occur in "leetcode", so we return -1. |
| 39 | + |
| 40 | +### Constraints |
| 41 | + |
| 42 | +- `1 <= haystack.length , needle.length <= 10^4 ` |
| 43 | +- haystack and needle consist of only lowercase English characters. |
| 44 | + |
| 45 | +### Approach |
| 46 | + |
| 47 | +To solve the problem, we can use the following approach: |
| 48 | + |
| 49 | +1. **Sliding Window Technique:** |
| 50 | + - We iterate through the `haystack` string with a window of size equal to the length of the `needle`. |
| 51 | + - At each step, we check if the substring of the `haystack` starting at the current index and having the same length as the `needle` matches the `needle`. |
| 52 | + - If a match is found, we return the current index. |
| 53 | + - If no match is found after iterating through the entire `haystack`, we return -1. |
| 54 | + |
| 55 | +### Solution Code |
| 56 | + |
| 57 | +#### Python |
| 58 | + |
| 59 | +``` |
| 60 | +class Solution(object): |
| 61 | + def strStr(self, haystack, needle): |
| 62 | + if not needle: |
| 63 | + return 0 |
| 64 | + if len(needle) > len(haystack): |
| 65 | + return -1 |
| 66 | + |
| 67 | + for i in range(len(haystack) - len(needle) + 1): |
| 68 | + if haystack[i:i + len(needle)] == needle: |
| 69 | + return i |
| 70 | + return -1 |
| 71 | +``` |
| 72 | + |
| 73 | +#### Java |
| 74 | + |
| 75 | +``` |
| 76 | +class Solution { |
| 77 | + public int strStr(String haystack, String needle) { |
| 78 | + if (needle.isEmpty()) return 0; |
| 79 | + |
| 80 | + for (int i = 0; i <= haystack.length() - needle.length(); i++) { |
| 81 | + if (haystack.substring(i, i + needle.length()).equals(needle)) { |
| 82 | + return i; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return -1; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +#### C++ |
| 92 | + |
| 93 | +``` |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + int strStr(string haystack, string needle) { |
| 97 | + if (needle.empty()) return 0; |
| 98 | + |
| 99 | + for (int i = 0; i <= haystack.length() - needle.length(); i++) { |
| 100 | + if (haystack.substr(i, needle.length()) == needle) { |
| 101 | + return i; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return -1; |
| 106 | + } |
| 107 | +}; |
| 108 | +``` |
| 109 | + |
| 110 | +### Conclusion |
| 111 | + |
| 112 | +The above solution efficiently finds the index of the first occurrence of a substring `needle` in the string `haystack`. It employs a sliding window technique to traverse the `haystack` string and compare substrings with the `needle`, providing a simple yet effective approach to solving the problem. |
0 commit comments