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.
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
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
Top Persistent Systems Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Persistent Systems Software Engineer
Reviews
Interviews
Salaries
Users/Month