Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up
Problem Submissions Solution

First Occurrence In A String

Difficulty: Medium

Acceptance: 50.08%

Points: 30.00

You are given two strings, **text** and **pattern**. Determine the first index in **text** where **pattern** appears as an exact substring. - If pattern exists in text, return the starting index of its first occurrence. - If pattern does not exist in text, return **-1.**

Topics

Companies

Articles

Examples:

Expected Time Complexity: O(|text| * |pattern|)

Expected Space Complexity: O(1)

Constraints:
  • 1 <= |text|,|pattern| <= 10^3
  • text and pattern consists of lowercase alphabets.
Companies:
Amazon Walmart TCS Infosys Wipro
Topics:
String
Locked Content
Access Restricted: Please Login to access the code editor and test cases.