Index of First Occurrence Problem Statement

Given two strings A and B, determine the index of the first occurrence of A in B. If A is not present in B, return -1.

Example:

Input:
A = "bc", B = "abcddbc"
Output:
1
Explanation:

The string A appears at index 1 and 5 in B (0-based index), but the first occurrence is at index 1.

Constraints:

  • 1 <= T <= 100
  • 1 <= |A|, |B| <= 5 * 104
  • Time limit: 1 second

Note:

You do not need to print anything. It has already been taken care of. Just implement the given function.

AnswerBot
4mo

Find the index of the first occurrence of string A in string B.

  • Iterate through string B and check if a substring of length equal to A matches A.

  • Return the index of the first occurrence of A in B, or -...read more

Arghya Pal
2y

let a = 'bc';

let b = 'abcddbc';

let bArray = b.split("");

let aArray = a.split("");

console.log(bArray);

var flag = 0;

for(let i=0; i<bArray.length; i++){

if(bArray[i] == aArray[0] && bArray[i+1] == aArray...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Persistent Systems

Q. What is the difference between an Interface and an abstract class?
Q. What is a virtual function?
Q. Write a program that prints a star pattern.
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits