|
| 1 | +# Find the Index of the First Occurrence in a String Problem |
| 2 | + |
| 3 | +## Problem Description: |
| 4 | + |
| 5 | +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`. |
| 6 | + |
| 7 | +### Example 1: |
| 8 | +- **Input:** haystack = "sadbutsad", needle = "sad" |
| 9 | +- **Output:** 0 |
| 10 | +- **Explanation:** "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. |
| 11 | +- |
| 12 | +### Example 2: |
| 13 | +- **Input:** haystack = "leetcode", needle = "leeto" |
| 14 | +- **Output:** -1 |
| 15 | +- **Explanation:** "leeto" did not occur in "leetcode", so we return -1. |
| 16 | +- |
| 17 | +### Constraints: |
| 18 | +- `1 <= haystack.length, needle.length <= 104` |
| 19 | +- haystack and needle consist of only lowercase English characters. |
| 20 | + |
| 21 | +## Problem Understanding: |
| 22 | + |
| 23 | +### ⭐️ Solution 1 ⭐️ |
| 24 | + |
| 25 | +#### Approach: |
| 26 | + |
| 27 | +- Iterate through each character of the haystack. |
| 28 | + |
| 29 | +- For each character, check if the substring starting at that position matches the needle character by character. |
| 30 | + |
| 31 | +- If a match is found, return the starting index; otherwise, continue until the end of the haystack. |
| 32 | + |
| 33 | +#### Pros: |
| 34 | + |
| 35 | +- Simple logic that is easy to understand. |
| 36 | + |
| 37 | +- Works well for short strings. |
| 38 | + |
| 39 | +#### Cons: |
| 40 | + |
| 41 | +- Less readable due to nested loops. |
| 42 | + |
| 43 | +- Inefficient for large strings. |
| 44 | + |
| 45 | +### Time Complexity: |
| 46 | + |
| 47 | +- Worst-case: O(N * M), where N is the length of the haystack and M is the length of the needle. |
| 48 | + |
| 49 | +- This occurs when each character of the haystack is checked against every character of the needle. |
| 50 | + |
| 51 | +### Space Complexity: |
| 52 | + |
| 53 | +- O(1) since no additional space is used except a few variables. |
| 54 | + |
| 55 | +### ⭐️ Solution 2 ⭐️ |
| 56 | + |
| 57 | +#### Approach: |
| 58 | + |
| 59 | +- Use slicing to extract substrings from the haystack. |
| 60 | + |
| 61 | +- Compare each substring of the same length as the needle with the needle itself. |
| 62 | + |
| 63 | +- If a match is found, return the starting index. |
| 64 | + |
| 65 | +#### Pros: |
| 66 | + |
| 67 | +- Improved readability and cleaner code. |
| 68 | + |
| 69 | +- Easier to maintain and debug. |
| 70 | + |
| 71 | +#### Cons: |
| 72 | + |
| 73 | +- Slight overhead due to creating substrings. |
| 74 | + |
| 75 | +### Time Complexity: |
| 76 | + |
| 77 | +- Worst-case: O(N * M), where N is the length of the haystack and M is the length of the needle. |
| 78 | + |
| 79 | +- Similar to Solution 1, but the slicing operation can introduce additional overhead. |
| 80 | + |
| 81 | +### Space Complexity: |
| 82 | + |
| 83 | +- O(M) since each slicing operation creates a substring of length M. |
| 84 | + |
| 85 | +### Conclusion |
| 86 | +Both solutions have the same time complexity. However, Solution 2 is more readable and maintainable due to its clean slicing approach. |
| 87 | +The slight space overhead is usually acceptable for most applications. Therefore, Solution 2 is recommended for real-world usage, unless memory constraints are critical. |
| 88 | +So i choose the solution 2. |
| 89 | + |
0 commit comments