Find the index of the first occurrence in a string | Leetcode 28

preview_player
Показать описание
Find the index of the first occurrence in a string
Leetcode problem number 28

JAVA interview programming playlist:

#java #interviewquestions #leetcode
Рекомендации по теме
Комментарии
Автор

class Solution {
public int strStr(String haystack, String needle) {
if (haystack.contains(needle)) {
return haystack.indexOf(needle);
} else {
return -1;
}
}
}

shreyasjain
Автор

This isn't an efficient solution. The complexity can be reduced to O(m + n) using KMP. Giving this answer may not fail you in the interview though but will not get you the best score either.

RishinderRana
Автор

Hey,

Thank you so much for now, I solve the problem of LeetCode, nice explanation 😊

BlendBox
Автор

Very useful content for Learning please subscribe and like share guys ❤😊😊

sangameshwar
Автор

Can you solve this problem with robin carp rolling hash?

arafathossain
Автор

Can you please explain more at timestamp 4:45 that why did you not take the whole length and just till index 7?

DevbrathBhattPujari
Автор

Can you please explain more at timestamp 4:45 that why did you just took the string till index 7?

DevbrathBhattPujari
Автор

You are doing a great job, keep it up, but its a request please try to include the most efficient approach, like KMP algorithm thats required here with space and time complexity.

kumar_shanu
Автор

very easy and helpful but Mam its time is O(m*n).Is there any other method in JS for O(m+n)?

AkashSharmaSDE
Автор

class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}

single line code for this problem

vikaspathak
Автор

smooth explanation
it would be helpelful if you can discuss time and space complexity with the approach.

madhur
Автор

this code will fail for the following input: haystack="hello" and needle = "ll"
if we do needle.length+1 then (i, needle.length)+1 will not give the correct substring as it will go from (2, 5)
it will include the last character as well "llo" which will result in wrong output
please correct me if i am wrong...

rishabhduggar
Автор

great explanation! Thanks for the help

saurabhtiwari
Автор

Thank you for this amazing explanation

MindsetMotivation
Автор

same approach i used in leetcode but it will give TLE for sure

shanayagupta
Автор

you are doing great work but please try to come up with at least 2 approaches like one will be brute force and other will be the optimal solution

chakravarthybatna
Автор

this is of O(mn) complexity, any other optimized solution?

TharunReddy
Автор

Explanation was nice but can we use methods like .charAt(), .substring() or .indexOf() in leetcode ?? Did you try entering this solution in leetcode . It doesn't work

harnekarora
Автор

Hey can anyone explain me how does "sad" comes at 0th index of "sadbutsad" word? Thanks

sandeepnarwal
Автор

This logic fails when haystack.length is less than needle's

SanjayChandramohan-lqwp