Close
Topic list
Implement strStr()
MEDIUM
25 mins
Strings
Topics (Covered in this problem)
Problem solved
Skill meter
Strings
-
-
Other topics
Problem solved
Skill meter
Matrices (2D Arrays)
-
-
-
-
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
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
0/80
Avg time to solve 25 mins
Success Rate 70 %
Share

## 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”.
``````
``````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
``````-1