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

Sdílet
Vložit
  • čas přidán 19. 09. 2022
  • Find the index of the first occurrence in a string
    Leetcode problem number 28
    JAVA interview programming playlist:
    • Interview Programming ...
    #java #interviewquestions #leetcode

Komentáře • 66

  • @anuragtripathi6609
    @anuragtripathi6609 Před rokem

    Need More of your videos ! Great Work

  • @saurabhtiwari5731
    @saurabhtiwari5731 Před rokem

    great explanation! Thanks for the help

  • @MindsetMotivation75
    @MindsetMotivation75 Před rokem

    Thank you for this amazing explanation

  • @BlendBox
    @BlendBox Před 4 měsíci

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

  • @kumar_shanu
    @kumar_shanu Před rokem +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  Před rokem

      Sure..Thanks for your suggestion..Will make a separate video on that approach as well..

  • @yxs7
    @yxs7 Před 11 měsíci

    u are the best. thks! ❤👍

  • @agyaani8060
    @agyaani8060 Před rokem

    Loved it❤✨

  • @sangameshwar78
    @sangameshwar78 Před 3 měsíci +1

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

  • @imvishu09
    @imvishu09 Před 6 měsíci

    really great!!

  • @madhur7787
    @madhur7787 Před rokem +1

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

    • @TechnosageLearning
      @TechnosageLearning  Před rokem +1

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

  • @DevbrathBhattPujari
    @DevbrathBhattPujari Před 16 dny

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

  • @vikashgupta4775
    @vikashgupta4775 Před rokem

    Thanks❤️

  • @AkashSharma-lf5jq
    @AkashSharma-lf5jq Před 9 měsíci +1

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

  • @DevbrathBhattPujari
    @DevbrathBhattPujari Před 16 dny

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

  • @shreyasjain1509
    @shreyasjain1509 Před rokem +11

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

    • @ayantika_paul
      @ayantika_paul Před 8 měsíci +1

      This one is the best solution.

    • @sagarmatha04
      @sagarmatha04 Před 6 měsíci +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 Před 5 měsíci

      This solves all the test cases (y)

    • @thewayofshaad4344
      @thewayofshaad4344 Před měsícem

      just one line
      return haystack.indexOf(needle);

  • @arafathossain6207
    @arafathossain6207 Před rokem +2

    Can you solve this problem with robin carp rolling hash?

  • @RishinderRana
    @RishinderRana Před rokem +7

    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  Před rokem +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 Před rokem

      True

  • @TharunReddy18
    @TharunReddy18 Před rokem

    Thnaks

  • @sarthakbhargava3514
    @sarthakbhargava3514 Před 11 měsíci

    Thanks

  • @vasugaur1283
    @vasugaur1283 Před 5 měsíci

    Best😇 Explaination

  • @codecurrents
    @codecurrents Před 7 měsíci

    Perfect 💕

  • @ankittripathi5796
    @ankittripathi5796 Před 4 měsíci

    Thanks mam

  • @AllInOne-dq9re
    @AllInOne-dq9re Před 5 měsíci

    nice

  • @TharunReddy18
    @TharunReddy18 Před rokem +1

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

    • @TechnosageLearning
      @TechnosageLearning  Před rokem +1

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

  • @shanayagupta4002
    @shanayagupta4002 Před rokem +1

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

    • @TechnosageLearning
      @TechnosageLearning  Před rokem

      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 Před rokem

      @@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  Před rokem

      I have started discussing complexities in recent videos..Thanks for the suggestion..Will definitely consider it

  • @chakravarthybatna1589
    @chakravarthybatna1589 Před 11 měsíci +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

  • @rishabhduggar2837
    @rishabhduggar2837 Před 9 měsíci +1

    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 Před 2 dny

      Ur wrong you have to sum the i to the length of needle; if(haystack.substring(i, i+ needle.length) === needle){...}

  • @harnekarora5658
    @harnekarora5658 Před 10 měsíci

    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  Před 10 měsíci

      I have another video on using indexOf() to solve this problem..Please check that..It's submitting and working in leetcode successfully

    • @harnekarora5658
      @harnekarora5658 Před 10 měsíci

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

  • @vikaspathak8706
    @vikaspathak8706 Před rokem +3

    class Solution {
    public int strStr(String haystack, String needle) {
    return haystack.indexOf(needle);
    }
    }
    single line code for this problem

    • @TechnosageLearning
      @TechnosageLearning  Před rokem

      Yes..That is also one of the approach to solve this problem...will make a separate video on that

    • @SAHIL-ud1gk
      @SAHIL-ud1gk Před 11 měsíci

      Thanks Bro

  • @Omneeshkushwah9695
    @Omneeshkushwah9695 Před rokem

    Please upload daily leetcode problems

  • @sandeepnarwal8782
    @sandeepnarwal8782 Před 11 měsíci

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

    • @whois2517
      @whois2517 Před 11 měsíci

      it starts from 0 ,1 then till n so in case of sad - s=0, a=1,d=2

  • @akgpian
    @akgpian Před 6 měsíci

    It will be O(MN) right ?

  • @sahilbambardestudy1469
    @sahilbambardestudy1469 Před 9 měsíci

    code plz!😀

  • @SanjayChandramohan-lq3wp
    @SanjayChandramohan-lq3wp Před 9 měsíci

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

  • @knixkcodes
    @knixkcodes Před rokem

    Sorry didn't understand. Please decease the pace.

    • @Rob-J-BJJ
      @Rob-J-BJJ Před rokem +3

      theres a replay button my brother

  • @gauravkumarkalra1156
    @gauravkumarkalra1156 Před 11 měsíci

    This solution will get failed if
    haystack ="mississippi"
    needle ="issip"

    • @TechnosageLearning
      @TechnosageLearning  Před 11 měsíci

      Hi..It will not fail..it will return the index as 4...Please try

  • @Naveenkumar-p4i
    @Naveenkumar-p4i Před měsícem

    here's a single word 0ms code for it
    class Solution {
    public int strStr(String haystack, String needle) {
    return haystack.indexOf(needle);
    }
    }

  • @ChaudharySaab05
    @ChaudharySaab05 Před rokem

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

  • @hybi666
    @hybi666 Před rokem

    Thanks