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.
Hello, Thankyou for the feedback. There are multiple ways to solve a problem, the aim is to get through the problem and to make everyone familiarize with an easy approach. Not every approach can be discussed or target a specific need.
indexOf already returns -1 if it doesnt find match. So you can jsut do this. public int strStr(String haystack, String needle) { return haystack.indexOf(needle); }
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.
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...
No..it is not giving TLE..it is accepted solution..Please try it out..Alternatively..you can use "indexOf" function to solve this problem..Will upload the new video soon using indexOf function
@@TechnosageLearning okay thank you, please also tell time complexity and space complexity in solution video, thank you and ma'am here's a suggestion please try to makes makes on leetcode daily challenges.
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
@@TechnosageLearning ok I'll do but don't know why leetcode didn't except.indexof() even before seeing your solution I tried that method and it's the simplest one line solution but leetcode didn't allow it
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.
Hello,
Thankyou for the feedback.
There are multiple ways to solve a problem, the aim is to get through the problem and to make everyone familiarize with an easy approach. Not every approach can be discussed or target a specific need.
True
class Solution {
public int strStr(String haystack, String needle) {
if (haystack.contains(needle)) {
return haystack.indexOf(needle);
} else {
return -1;
}
}
}
This one is the best solution.
indexOf already returns -1 if it doesnt find match. So you can jsut do this.
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
This solves all the test cases (y)
just one line
return haystack.indexOf(needle);
Very useful content for Learning please subscribe and like share guys ❤😊😊
Hey,
Thank you so much for now, I solve the problem of LeetCode, nice explanation 😊
class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
single line code for this problem
Yes..That is also one of the approach to solve this problem...will make a separate video on that
Thanks Bro
Can you please explain more at timestamp 4:45 that why did you not take the whole length and just till index 7?
Can you solve this problem with robin carp rolling hash?
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.
Sure..Thanks for your suggestion..Will make a separate video on that approach as well..
Can you please explain more at timestamp 4:45 that why did you just took the string till index 7?
Best😇 Explaination
smooth explanation
it would be helpelful if you can discuss time and space complexity with the approach.
Thanks for the suggestion ! Will definitely start discussing space and time complexity in the upcoming videos.
very easy and helpful but Mam its time is O(m*n).Is there any other method in JS for O(m+n)?
you should take the length of needle no???
Need More of your videos ! Great Work
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...
Ur wrong you have to sum the i to the length of needle; if(haystack.substring(i, i+ needle.length) === needle){...}
same approach i used in leetcode but it will give TLE for sure
No..it is not giving TLE..it is accepted solution..Please try it out..Alternatively..you can use "indexOf" function to solve this problem..Will upload the new video soon using indexOf function
@@TechnosageLearning okay thank you, please also tell time complexity and space complexity in solution video, thank you and ma'am here's a suggestion please try to makes makes on leetcode daily challenges.
I have started discussing complexities in recent videos..Thanks for the suggestion..Will definitely consider it
u are the best. thks! ❤👍
really great!!
this is of O(mn) complexity , any other optimized solution?
Alternatively you can use JAVA method indexOf to reduce the time complexity to O(n)
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
I have another video on using indexOf() to solve this problem..Please check that..It's submitting and working in leetcode successfully
@@TechnosageLearning ok I'll do but don't know why leetcode didn't except.indexof() even before seeing your solution I tried that method and it's the simplest one line solution but leetcode didn't allow it
great explanation! Thanks for the help
Thank you for this amazing explanation
It will be O(MN) right ?
(n-m) m
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
Sure
Perfect 💕
Loved it❤✨
code plz!😀
Please upload daily leetcode problems
Hello Avneesh,
We are trying our best. Please stay connected 🙏
Thanks
Thanks❤️
Superb dear, fully mind-blowing approach!!
@technosage
Hey can anyone explain me how does "sad" comes at 0th index of "sadbutsad" word? Thanks
it starts from 0 ,1 then till n so in case of sad - s=0, a=1,d=2
Thanks mam
Thnaks
Sorry didn't understand. Please decease the pace.
theres a replay button my brother
This logic fails when haystack.length is less than needle's
nice
This solution will get failed if
haystack ="mississippi"
needle ="issip"
Hi..It will not fail..it will return the index as 4...Please try
here's a single word 0ms code for it
class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
Thanks