Implement indexOf Function

You are provided with two strings A and B. Your task is to find the index of the first occurrence of A within B. If A is not found in B, return -1.

Example:

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

The substring "bc" is located at indices 1 and 5 (0-based index) in B, with the first occurrence at index 1.

Constraints:

  • 1 <= T <= 100
  • 1 <= |A|, |B| <= 5 * 10^4
Input:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow. The first and only line of each test case contains two strings A and B, separated by a single space.
Output:
For each test case, print the index of the first occurrence of A in B. If string A is not present in string B then print -1.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
AnswerBot
2d

Implement a function to find the index of the first occurrence of one string within another string.

  • Iterate through the second string and check if a substring of the same length as the first string mat...read more

Help your peers!
Add answer anonymously...
Urban Company Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter