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

  Рет қаралды 45,634

Technosage

Technosage

Күн бұрын

Пікірлер: 70
@RishinderRana
@RishinderRana 2 жыл бұрын
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.
@TechnosageLearning
@TechnosageLearning 2 жыл бұрын
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.
@yashagrawal1663
@yashagrawal1663 Жыл бұрын
True
@arafathossain6207
@arafathossain6207 Жыл бұрын
Can you solve this problem with robin carp rolling hash?
@shreyasjain1509
@shreyasjain1509 Жыл бұрын
class Solution { public int strStr(String haystack, String needle) { if (haystack.contains(needle)) { return haystack.indexOf(needle); } else { return -1; } } }
@ayantika_paul
@ayantika_paul Жыл бұрын
This one is the best solution.
@sagarmatha04
@sagarmatha04 11 ай бұрын
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); }
@yashasks1749
@yashasks1749 10 ай бұрын
This solves all the test cases (y)
@thewayofshaad4344
@thewayofshaad4344 7 ай бұрын
just one line return haystack.indexOf(needle);
@atharvakulkarni1601
@atharvakulkarni1601 2 күн бұрын
This method is similar to find() in python which returns -1 if the substring is not found in the string
@DevbrathBhattPujari
@DevbrathBhattPujari 6 ай бұрын
Can you please explain more at timestamp 4:45 that why did you not take the whole length and just till index 7?
@sangameshwar78
@sangameshwar78 8 ай бұрын
Very useful content for Learning please subscribe and like share guys ❤😊😊
@DevbrathBhattPujari
@DevbrathBhattPujari 6 ай бұрын
Can you please explain more at timestamp 4:45 that why did you just took the string till index 7?
@kumar_shanu
@kumar_shanu Жыл бұрын
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.
@TechnosageLearning
@TechnosageLearning Жыл бұрын
Sure..Thanks for your suggestion..Will make a separate video on that approach as well..
@AkashSharmaSDE
@AkashSharmaSDE Жыл бұрын
very easy and helpful but Mam its time is O(m*n).Is there any other method in JS for O(m+n)?
@vikaspathak8706
@vikaspathak8706 Жыл бұрын
class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(needle); } } single line code for this problem
@TechnosageLearning
@TechnosageLearning Жыл бұрын
Yes..That is also one of the approach to solve this problem...will make a separate video on that
@SAHIL-ud1gk
@SAHIL-ud1gk Жыл бұрын
Thanks Bro
@rishabhduggar2837
@rishabhduggar2837 Жыл бұрын
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...
@saulcasaslorenzo2243
@saulcasaslorenzo2243 5 ай бұрын
Ur wrong you have to sum the i to the length of needle; if(haystack.substring(i, i+ needle.length) === needle){...}
@madhur7787
@madhur7787 2 жыл бұрын
smooth explanation it would be helpelful if you can discuss time and space complexity with the approach.
@TechnosageLearning
@TechnosageLearning 2 жыл бұрын
Thanks for the suggestion ! Will definitely start discussing space and time complexity in the upcoming videos.
@BlendBox
@BlendBox 9 ай бұрын
Hey, Thank you so much for now, I solve the problem of LeetCode, nice explanation 😊
@SrijanaSingh-f1b
@SrijanaSingh-f1b 16 күн бұрын
you should take the length of needle no???
@TharunReddy18
@TharunReddy18 2 жыл бұрын
this is of O(mn) complexity , any other optimized solution?
@TechnosageLearning
@TechnosageLearning 2 жыл бұрын
Alternatively you can use JAVA method indexOf to reduce the time complexity to O(n)
@Vishu-y8k
@Vishu-y8k Күн бұрын
do not do +1 in loop it can be make runtime error in test case like "aaa"
@anuragtripathi6609
@anuragtripathi6609 2 жыл бұрын
Need More of your videos ! Great Work
@vasugaur1283
@vasugaur1283 10 ай бұрын
Best😇 Explaination
@chakravarthybatna1589
@chakravarthybatna1589 Жыл бұрын
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
@TechnosageLearning
@TechnosageLearning Жыл бұрын
Sure
@yxs7
@yxs7 Жыл бұрын
u are the best. thks! ❤👍
@shanayagupta4002
@shanayagupta4002 Жыл бұрын
same approach i used in leetcode but it will give TLE for sure
@TechnosageLearning
@TechnosageLearning Жыл бұрын
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
@shanayagupta4002
@shanayagupta4002 Жыл бұрын
@@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.
@TechnosageLearning
@TechnosageLearning Жыл бұрын
I have started discussing complexities in recent videos..Thanks for the suggestion..Will definitely consider it
@akgpian
@akgpian 11 ай бұрын
It will be O(MN) right ?
@foudilbenouci482
@foudilbenouci482 8 ай бұрын
(n-m) m
@imvishu09
@imvishu09 11 ай бұрын
really great!!
@harnekarora5658
@harnekarora5658 Жыл бұрын
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
@TechnosageLearning Жыл бұрын
I have another video on using indexOf() to solve this problem..Please check that..It's submitting and working in leetcode successfully
@harnekarora5658
@harnekarora5658 Жыл бұрын
@@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
@hybi666
@hybi666 Жыл бұрын
Thanks
@codecurrents
@codecurrents Жыл бұрын
Perfect 💕
@MindsetMotivation75
@MindsetMotivation75 2 жыл бұрын
Thank you for this amazing explanation
@saurabhtiwari5731
@saurabhtiwari5731 2 жыл бұрын
great explanation! Thanks for the help
@vikash4775
@vikash4775 2 жыл бұрын
Thanks❤️
@agyaani8060
@agyaani8060 2 жыл бұрын
Loved it❤✨
@ankittripathi5796
@ankittripathi5796 10 ай бұрын
Thanks mam
@TharunReddy18
@TharunReddy18 2 жыл бұрын
Thnaks
@sahilbambardestudy1469
@sahilbambardestudy1469 Жыл бұрын
code plz!😀
@sandeepnarwal8782
@sandeepnarwal8782 Жыл бұрын
Hey can anyone explain me how does "sad" comes at 0th index of "sadbutsad" word? Thanks
@whois2517
@whois2517 Жыл бұрын
it starts from 0 ,1 then till n so in case of sad - s=0, a=1,d=2
@Omneeshkushwah9695
@Omneeshkushwah9695 2 жыл бұрын
Please upload daily leetcode problems
@TechnosageLearning
@TechnosageLearning 2 жыл бұрын
Hello Avneesh, We are trying our best. Please stay connected 🙏
@AllInOne-dq9re
@AllInOne-dq9re 11 ай бұрын
nice
@knixkcodes
@knixkcodes Жыл бұрын
Sorry didn't understand. Please decease the pace.
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
theres a replay button my brother
@SanjayChandramohan-lq3wp
@SanjayChandramohan-lq3wp Жыл бұрын
This logic fails when haystack.length is less than needle's
@gauravkumarkalra1156
@gauravkumarkalra1156 Жыл бұрын
This solution will get failed if haystack ="mississippi" needle ="issip"
@TechnosageLearning
@TechnosageLearning Жыл бұрын
Hi..It will not fail..it will return the index as 4...Please try
@Naveenkumar-p4i
@Naveenkumar-p4i 6 ай бұрын
here's a single word 0ms code for it class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(needle); } }
@ChaudharySaab05
@ChaudharySaab05 Жыл бұрын
Superb dear, fully mind-blowing approach!! @technosage
@sarthakbhargava3514
@sarthakbhargava3514 Жыл бұрын
Thanks
Is Subsequence | LeetCode problem 392
6:41
Technosage
Рет қаралды 6 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 909 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 457 М.
Why OOP is evolving(and why it's a good thing)
7:35
TMF
Рет қаралды 16 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 679 М.
Reverse words in a string | Leetcode problem 151
6:08
Technosage
Рет қаралды 48 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 994 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН