Interview question: Find the Index of the First Occurrence in a String

Problem

You are given a string s and a substring sub. Your task is to find the index of the first occurrence of sub in s. If sub is not a substring of s, return -1.

Example 1

Input: s = "hello world", sub = "l"
Result: 2

Example 2

Input: s = "abcdefg", sub = "x"
Result: -1

Example 3

Input: s = "mississippi", sub = "iss"
Result: 1

Test

Results