Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Find the Index of the First Occurrence in a String

Easy
stringtwo-pointerskmp

Return the index of the first occurrence of needle in haystack, or -1 if absent.

Input

  • Line 1: haystack
  • Line 2: needle

Output: the index, or -1.

Example 1
Input:
sadbutsad
sad
Output:
0
Example 2
Input:
leetcode
leeto
Output:
-1

Constraints

  • 1 ≤ lengths ≤ 10^4
  • Lowercase English letters.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.