Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Implement strStr()
MEDIUM
25 mins
4 upvotes
Strings
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Implement strStr()

Contributed by
Shivam
Medium
yellow-spark
0/80
Avg time to solve 25 mins
Success Rate 70 %
Share
4 upvotes

Problem Statement

You are given two strings A and B. Find the index of the first occurrence of A in B. If A is not present in B, then return -1.

For Example:
A = “bc”, B = “abcddbc”.
String “A” is present at index 1, and 5(0-based index), but we will return 1 as it is the first occurrence of “A” in string “B”.
Follow Up:
Can you solve this in linear time and space complexity?
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
ninjas codingninjas
code codingninjas
Sample Output 1:
6
-1
Explanation For Sample Input 1:
For the first test case, “ninjas” is present at the 6th index of “codingninjas”.

For the second test case, “code” is not present in “codingninjas”.
Sample Input 2:
2
e add
en engagement
Sample output 2:
-1
0
Reset Code
Full screen
Auto
copy-code
Console