Skip to content

85 Determine If One String Is Another's Substring (Lai)

Determine if a small string is a substring of another large string.

Return the index of the first occurrence of the small string in the large string.

Return -1 if the small string is not a substring of the large string.

Assumptions

  • Both large and small are not null
  • If small is empty string, return 0

Examples

  • “ab” is a substring of “bcabc”, return 2
  • “bcd” is not a substring of “bcabc”, return -1
  • "" is substring of "abc", return 0