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

Поділитися
Вставка
  • Опубліковано 11 січ 2025

КОМЕНТАРІ • 68

  • @RishinderRana
    @RishinderRana 2 роки тому +10

    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 роки тому +7

      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

  • @shreyasjain1509
    @shreyasjain1509 Рік тому +13

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

    • @ayantika_paul
      @ayantika_paul Рік тому +1

      This one is the best solution.

    • @sagarmatha04
      @sagarmatha04 11 місяців тому +2

      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);

  • @sangameshwar78
    @sangameshwar78 8 місяців тому +1

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

  • @BlendBox
    @BlendBox 9 місяців тому

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

  • @vikaspathak8706
    @vikaspathak8706 Рік тому +3

    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

  • @DevbrathBhattPujari
    @DevbrathBhattPujari 5 місяців тому +1

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

  • @arafathossain6207
    @arafathossain6207 Рік тому +3

    Can you solve this problem with robin carp rolling hash?

  • @kumar_shanu
    @kumar_shanu Рік тому +2

    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..

  • @DevbrathBhattPujari
    @DevbrathBhattPujari 5 місяців тому

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

  • @vasugaur1283
    @vasugaur1283 10 місяців тому

    Best😇 Explaination

  • @madhur7787
    @madhur7787 2 роки тому +1

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

    • @TechnosageLearning
      @TechnosageLearning  2 роки тому +1

      Thanks for the suggestion ! Will definitely start discussing space and time complexity in the upcoming videos.

  • @AkashSharmaSDE
    @AkashSharmaSDE Рік тому +1

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

  • @SrijanaSingh-f1b
    @SrijanaSingh-f1b 13 днів тому

    you should take the length of needle no???

  • @anuragtripathi6609
    @anuragtripathi6609 2 роки тому

    Need More of your videos ! Great Work

  • @rishabhduggar2837
    @rishabhduggar2837 Рік тому +2

    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){...}

  • @shanayagupta4002
    @shanayagupta4002 Рік тому +1

    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

  • @yxs7
    @yxs7 Рік тому

    u are the best. thks! ❤👍

  • @imvishu09
    @imvishu09 11 місяців тому

    really great!!

  • @TharunReddy18
    @TharunReddy18 2 роки тому +1

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

    • @TechnosageLearning
      @TechnosageLearning  2 роки тому +1

      Alternatively you can use JAVA method indexOf to reduce the time complexity to O(n)

  • @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

  • @saurabhtiwari5731
    @saurabhtiwari5731 2 роки тому

    great explanation! Thanks for the help

  • @MindsetMotivation75
    @MindsetMotivation75 2 роки тому

    Thank you for this amazing explanation

  • @akgpian
    @akgpian 11 місяців тому

    It will be O(MN) right ?

  • @chakravarthybatna1589
    @chakravarthybatna1589 Рік тому +1

    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

  • @codecurrents
    @codecurrents Рік тому

    Perfect 💕

  • @agyaani8060
    @agyaani8060 2 роки тому

    Loved it❤✨

  • @sahilbambardestudy1469
    @sahilbambardestudy1469 Рік тому

    code plz!😀

  • @Omneeshkushwah9695
    @Omneeshkushwah9695 2 роки тому

    Please upload daily leetcode problems

    • @TechnosageLearning
      @TechnosageLearning  2 роки тому

      Hello Avneesh,
      We are trying our best. Please stay connected 🙏

  • @hybi666
    @hybi666 Рік тому

    Thanks

  • @vikash4775
    @vikash4775 2 роки тому

    Thanks❤️

  • @ChaudharySaab05
    @ChaudharySaab05 Рік тому

    Superb dear, fully mind-blowing approach!!
    @technosage

  • @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

  • @ankittripathi5796
    @ankittripathi5796 10 місяців тому

    Thanks mam

  • @TharunReddy18
    @TharunReddy18 2 роки тому

    Thnaks

  • @knixkcodes
    @knixkcodes Рік тому

    Sorry didn't understand. Please decease the pace.

    • @Rob-J-BJJ
      @Rob-J-BJJ Рік тому +3

      theres a replay button my brother

  • @SanjayChandramohan-lq3wp
    @SanjayChandramohan-lq3wp Рік тому

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

  • @AllInOne-dq9re
    @AllInOne-dq9re 11 місяців тому

    nice

  • @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);
    }
    }

  • @sarthakbhargava3514
    @sarthakbhargava3514 Рік тому

    Thanks