Find The Index Of The First Occurrence In A String

To solve this coding challenge of finding the index of the first occurrence of
needle
in
haystack
, we need to iterate carefully and identify where the substring
needle
first appears within
haystack
. The solution must return
-1
if
needle
is not found in
haystack
.
Here's a step-by-step explanation of the pseudocode to accomplish this task:

Explanation:

  1. Initialize Variables : Begin by checking for corner cases such as the
    needle
    being an empty string. If
    needle
    is empty, return
    0
    immediately as it is technically found at the beginning.
  2. Check if
    needle
    is in
    haystack
    : Before performing the search, check if
    needle
    is even present within
    haystack
    . If
    needle
    is not present, return
    -1
    .
  3. Iterate Through
    haystack
    : Traverse
    haystack
    using an index until a window size comparable to the length of
    needle
    is reached.
  4. Compare Substrings : For each index, extract the substring of
    haystack
    which is of the same length as
    needle
    and compare this substring with
    needle
    .
  5. Return First Match : If a matching substring is found, return its starting index. If loop concludes without finding a match, return
    -1
    .
  6. Detailed Steps in Pseudocode:

                                                
    # Function to find first occurrence of needle in haystack
    FUNCTION findFirstOccurrence(haystack, needle)
    
    # Step 1: Handle edge cases where needle is empty
    IF needle IS EMPTY
    RETURN 0
    
    # Step 2: Check if needle is not part of haystack at all
    IF needle NOT IN haystack
    RETURN -1
    
    # Step 3: Iterate through haystack with index i
    FOR i FROM 0 TO (length of haystack - length of needle)
    # Extract substring of haystack
    substring = haystack[i : i + length of needle]
    
    # Step 4: Compare extracted substring with needle
    IF substring EQUALS needle
    RETURN i  # Return the start index of the first occurrence
    
    # Step 5: If no match is found, return -1
    RETURN -1
    
                                            
    Here’s a breakdown of the steps to ensure clarity:
  7. Initiate a function
    findFirstOccurrence
    that takes
    haystack
    and
    needle
    strings as parameters.
  8. Check if
    needle
    is an empty string; if so, return
    0
    because an empty
    needle
    is always technically found at the start.
  9. Use a direct membership check (
    needle
    in
    haystack
    ) to see if
    needle
    is even part of
    haystack
    . If not, return
    -1
    .
  10. Iterate over
    haystack
    from the start index up to the end position where a full-length
    needle
    substring can still be compared. This means iterating from
    0
    to
    length of haystack - length of needle
    .
  11. For every iteration, extract a substring from
    haystack
    starting at the current index
    i
    of length equivalent to
    needle
    .
  12. Compare this substring to
    needle
    . If a match is found, return the current index
    i
    .
  13. If the loop completes without finding any match, return
    -1
    indicating
    needle
    does not occur within
    haystack
    .
This detailed approach ensures that all edge cases and possible input scenarios are covered, offering a methodical way to solve the challenge.