/28. Find First Substring Occurrence Index

28. Find First Substring Occurrence Index

Easy
Two Pointers46.3% acceptance

Given two strings, source_string and target_substring, return the index of the first occurrence of target_substring in source_string. If target_substring does not occur in source_string, return -1.

Example 1

Input: source_string=abcabcabc, target_substring=cab

Output: 2

Explanation: The substring cab first appears at index 2 in abcabcabc.

Example 2

Input: source_string=abcdefgh, target_substring=xyz

Output: -1

Explanation: The substring xyz does not appear in abcdefgh.

Constraints

  • 1 <= len(source_string) <= 104
  • 1 <= len(target_substring) <= 104
  • source_string and target_substring consist only of lowercase English letters
Python (current runtime)

Case 1

Input: source_string='mississippi', target_substring='issi'

Expected: 1

Case 2

Input: source_string='aabbccdd', target_substring='bbcc'

Expected: 2

Case 3

Input: source_string='zzzzzz', target_substring='zzz'

Expected: 0

Case 4

Input: source_string='abcdefg', target_substring='fg'

Expected: 5

Case 5

Input: source_string='qwerty', target_substring='tyu'

Expected: -1