Design Gurus Logo
Find the Index of the First Occurrence in a String (easy)

Problem Statement

Given two strings str1 and str2, return the index of first occurrences of the str2 string in str1. If str2 doesn't exists in str1 string, return -1.

Examples

  • Example 1:

    • Input: str1 = "algorithms", str2 = "rith"
    • Expected Output: 4
    • Justification: The substring "rith" begins at index 4 in the string "algorithms".
  • Example 2:

    • Input: str1 = "openai", str2 = "ai"
    • Expected Output: 4
    • Justification: The substring "ai" starts at index 4 in the string "openai".
  • Example 3:

    • Input: str1 = "challenge", str2 = "test"
    • Expected Output: -1
    • Justification: The substring "test" does not appear in the string "challenge", hence the output is -1.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory