Find the Index of the First Occurrence in a String | Google, Microsoft | Leetcode 28

preview_player
Показать описание
This is the 14th Video of our Strings Playlist.
In this video we will try to solve another very good and popular problem "Find the Index of the First Occurrence in a String - Leetcode 28"

Share your learnings on LinkedIn, Twitter (X), Instagram, Facebook(Meta) with the hashtag #codestorywithmik & feel free to tag me.

Problem Name : Find the Index of the First Occurrence in a String
Company Tags : Google, Amazon, Microsoft, Facebook, Qualcomm

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

Thanks, bhai. Below is the JAVA implementation:

class Solution {
public int strStr(String s1, String s2) {
int m = s1.length();
int n = s2.length();

for(int i=0; i<m-n+1; i++) {
for(int j=0; j<n; j++) {
if(s1.charAt(i+j) != s2.charAt(j)) {
break;
}
if(j == n-1) {
return i;
}
}
}
return -1;
}
}

JJ-tpdd
Автор

Your voice is good, the audio was clear and bold and you explained so well. Thank you.

aryansinha
Автор

congratulations on 8k subs. Please try to cover questions from popular SDE sheets that engineering students are following(these days) meanwhile I wish you all the best for your own SDE sheet.

I am following two things 1- POTD leetcode 2- a popular SDE sheet I found on the web. For both things, I first search for your solutions.

taneyasoni
Автор

Ur efforts is really really gold producers love u bhaiya ❤️❤️❤️❤️

HealthyOm
Автор

Aapke jaisa banna hai, aapki coding journey share karo pls

prathamtetgure
Автор

After the first for loop you can add an if condition which will check if first letter of needle is same as current letter of haystack, so that we won't go inside the second for loop, for each character of haystack.

itspravas
Автор

Well explained!!! Bro when are you uploading KMP and Rabin karp alg videos?

siddharthsinghbhadoria
Автор

Thanks for the explanation
Please provide videos of kmp and rabin karp

arunsharma
Автор

This is O(n) solution in java : 0ms runtime

class Solution {
public int strStr(String haystack, String needle) {
int n = haystack.length();
int strLen = needle.length();

for(int i=0; i<=n-strLen; i++){
if( haystack.substring(i, i+strLen).equals(needle) ){
return i;
}
}
return -1;
}
}

harsh.jain
Автор

(Minimum Fuel Cost to Report to the Capital )

Hii sir please make video of above question

rohitsingh
Автор

How's this one?

class Solution {
public:
int strStr(string haystack, string needle) {
int m = haystack.size();
int n = needle.size();
for(int i=0 ; i<m ; i++)
{
if(haystack.substr(i, n)==needle) return i;
}
return -1;

}
};

best
Автор

Could anyone share brute force approach?

csepratikshaargulewar
Автор

What is the complexity if we use while loop..
int strStr(string haystack, string needle) {
int m = haystack.length();
int n = needle.length();
int count = 0;
int i= 0, j=0;
while(i<m && j<n)
{
if(haystack[i+j] == needle[j])
{
count++;
if(count == n )
{
return i;
}
j++;
}
else
{
i++;
count = 0;
j = 0;
}
}
return -1;
}

arunsharma
Автор

for(int
if(s1.substring(i, i+s2.length()).equals(s2))
return i;


return -1;

naveenrajput
Автор

class Solution {
public int strStr(String haystack, String needle) {

return -1;
for(int
int j=i;
while(j-i<needle.length() && j<haystack.length() &&
j++;
if(j-i==needle.length())
return i;
}
return -1;
}
}

yuvrajbhardawaj
Автор

Sir please make a video on rabin karp algorithm please sir 😭

ektuhaso
Автор

this is also fine right ?
code :
class Solution {
public:
int strStr(string haystack, string needle) {
int n=needle.length();
int i=0;
int j=0;
while(i<haystack.length())
{
if(j==n)return i;
else
else{
i++;
j=0;
}
}
return -1;
}
};

vishalplayzz
Автор

vai kmp and rabin karp ki video bnado eagarly waiting for it

samir
Автор

12:14 agar dekha jaye to udhar se hi i return krega ....lekin internally kya loop chalega return ke bad v ?

ezcoding
Автор

Bhai can you tell me complexity of this code pleaseee
class Solution {

public int strStr(String haystack, String needle) {
String match = needle;
String ans = "";
int j=0;
int i=0;
while(j < haystack.length()){
ans += haystack.charAt(j);
if(j-i+1 < needle.length()) j++;
else if(match.equals(ans)){
return i;
}
else{
ans = ans.substring(1);
i++;
j++;
}
}
return -1;
}
}

yashgarg