Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Find the Index of the First Occurrence in a String

XPChallenge Points: 10
levelLevel: Easy

You are given two strings: text and pattern.

Return the index of the first place pattern appears in text.
If pattern does not appear, return -1.

Example 1:

Input: text = "hellohello", pattern = "llo"

Output: 2

Explanation:

"llo" first appears starting at index 2.

Example 2:

Input: text = "openai", pattern = "ai"

Output: 4

Explanation:

"ai" appears first at index 4.

Example 3:

Input: text = "python", pattern = "java"

Output: -1

Explanation:

java" is not in "python".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result