8. Longest Palindromic Substring | Leetcode 5 | Strings | Odd and even length approach

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024

Комментарии • 110

  • @AyushiSharmaDSA
    @AyushiSharmaDSA  2 месяца назад +1

    Sorry guys for interruption, checkout code explanation here: ruclips.net/video/9jnLi3UX-q0/видео.html

  • @whtthywnt4259
    @whtthywnt4259 2 года назад +19

    finally a non-dp approach !!

  • @arpitrajput6424
    @arpitrajput6424 2 года назад +19

    C++ code ->
    string longestPalindrome(string s) {

    int n = s.length();
    int low,high;

    int st=0 ;// this will store starting index of longest palindrome substring
    len=1; //this will store length of longest palindrome substring

    for(int i=1;i=0 && high len){//this condition is checking if we got better length of palindrome substring then store it
    st = low;
    len = high-low+1;
    }
    low--;high++;
    }

    //odd case
    low = i-1;
    high = i+1;
    while(low>=0 && high len){
    st = low;
    len = high-low+1;
    }
    low--;high++;
    }


    }

    return s.substr(st,len);

    }

    • @tusharnain6652
      @tusharnain6652 2 года назад +2

      you can start the for loop from 0 like this
      string longestPalindrome(string s)
      {
      int n = s.size();
      int low, high;
      int lps_start = 0;
      int lps_size = 1; // there will always be an lps of size 1 in any string
      for (int i = 0; i < n; i++)
      {
      // even case
      low = i, high = i + 1;
      while (low >= 0 and high < n and s[low] == s[high])
      {
      if (high - low + 1 > lps_size)
      {
      lps_start = low;
      lps_size = high - low + 1;
      }
      low--;
      high++;
      }
      // odd case
      low = i;
      high = i;
      while (low >= 0 and high < n and s[low] == s[high])
      {
      if (high - low + 1 > lps_size)
      {
      lps_start = low;
      lps_size = high - low + 1;
      }
      low--;
      high++;
      }
      }
      return s.substr(lps_start, lps_size);
      }

    • @Viralmems_xyz
      @Viralmems_xyz Год назад

      bro why wwe r not using if else for even or odd

    • @sayanacharyya250
      @sayanacharyya250 5 месяцев назад

      Very helpful bhaiya ❤

    • @himanshuu421
      @himanshuu421 4 месяца назад +1

      @@tusharnain6652 this one is so much more clearer , thanks man

  • @tusharnain6652
    @tusharnain6652 2 года назад +3

    //C++ solution and you can also start the for loop from 0
    string longestPalindrome(string s)
    {
    int n = s.size();
    int low, high;
    int lps_start = 0;
    int lps_size = 1; // there will always be an lps of size 1 in any string
    for (int i = 0; i < n; i++)
    {
    // even case
    low = i, high = i + 1;
    while (low >= 0 and high < n and s[low] == s[high])
    {
    if (high - low + 1 > lps_size)
    {
    lps_start = low;
    lps_size = high - low + 1;
    }
    low--;
    high++;
    }
    // odd case
    low = i;
    high = i;
    while (low >= 0 and high < n and s[low] == s[high])
    {
    if (high - low + 1 > lps_size)
    {
    lps_start = low;
    lps_size = high - low + 1;
    }
    low--;
    high++;
    }
    }
    return s.substr(lps_start, lps_size);
    }

    • @aisharawat9102
      @aisharawat9102 Год назад

      In substr function the second argument is maybe the endpoint of the substring exclusive. Can we pass the length of the substring as the second argument??

  • @Nilesh773
    @Nilesh773 Год назад

    class Solution {
    public String longestPalindrome(String s)
    {
    int n=s.length();
    int low,high;
    int pal_Start=0;
    int pal_Length=1; // any string atleast having size 1 palindrom
    for(int i=0;i=0 && highpal_Length)
    {
    pal_Start=low;
    pal_Length=high-low+1;
    }
    low--;
    high++;
    }
    //odd
    low=i;
    high=i;
    while(low>=0 && highpal_Length)
    {
    pal_Start=low;
    pal_Length=high-low+1;
    }
    low--;
    high++;
    }
    }
    System.out.println(pal_Start+"----"+pal_Length);
    return s.substring(pal_Start,pal_Start+pal_Length);

    }
    }

  • @maheshwarang4000
    @maheshwarang4000 11 месяцев назад

    Java solution :
    class Solution {
    public String longestPalindrome(String s) {
    int max = 1;
    int start = 0;
    int left ;
    int right;
    for (int i = 0; i < s.length(); i++) {
    // even
    left = i-1;
    right = i;
    while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
    if (max < right - left + 1) {
    start = left;
    max = right - left + 1;
    }
    left--;
    right++;
    }
    // odd
    left = i;
    right = i;
    while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
    if (max < right - left + 1) {
    start = left;
    max = right - left + 1;
    }
    left--;
    right++;
    }
    }
    return s.substring(start,start+max);
    }
    }

  • @mohitarora2190
    @mohitarora2190 2 года назад +2

    why did u stop at end ?

  • @sravanthinampelli9391
    @sravanthinampelli9391 Год назад

    Thank you ayushi 💕

  • @fahad_mihran2286
    @fahad_mihran2286 Год назад

    good work ayushi if i search for a problem i search wheather u have done it or not

  • @adithyaas7735
    @adithyaas7735 2 года назад

    underrated ! , awesome explaination..

  • @mohdkhaleeq7468
    @mohdkhaleeq7468 3 года назад

    Ayushi your videos are very helpful please continue solving love Babbar sheet problem do not stop

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +1

      Thank you so much :). Sure, I'll continue with sheet problems only :)

  • @aakashgupta7211
    @aakashgupta7211 2 года назад +1

    What will be the time complexity?

  • @pawankumarpandit1822
    @pawankumarpandit1822 Год назад

    thank you so much mam......

  • @AyushiSharmaDSA
    @AyushiSharmaDSA  3 года назад +3

    Register for the Scholarship Test here:bit.ly/3CpsLfH
    USE: SCTCJSGXLD TO GET 50% DISCOUNT ON THE REGISTRATION FEES

  • @masumali8356
    @masumali8356 2 месяца назад

    please give a thumbs up if you like
    int n=S.size();
    int len=1;
    int st=0;
    for(int i=1;i=0 && highlen)
    {
    st=low;
    len=high-low+1;
    }
    high++;
    low--;
    }
    low=i-1;
    high=i+1;
    while(low>=0 && highlen)
    {
    st=low;
    len=high-low+1;
    }
    high++;
    low--;
    }
    }
    return S.substr(st,len);

  • @fizataj6628
    @fizataj6628 Год назад

    @AyushiSharmaDSA
    Could u explain coding part ?

  • @vivekkumaragrawal3963
    @vivekkumaragrawal3963 2 года назад

    Thank you Di Great approach

  • @rishalirishu5833
    @rishalirishu5833 2 года назад +1

    great explanation

  • @satyamaditya384
    @satyamaditya384 Год назад

    Great explanation ma'am !!

  • @meme_engineering4521
    @meme_engineering4521 2 года назад

    please share O(N) approach

  • @anshusan2174
    @anshusan2174 2 года назад +2

    is the time complexity - O(N^2)?

  • @abhishekupadhyay2259
    @abhishekupadhyay2259 3 года назад +1

    Ayushi love babber ke 450 question solve krwa do Java mi, trust me it will great help for us

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +1

      Sure Abhishek, I don't give java code so that you guys write yourself. It will help you. But if you want I'll add java code too :)

    • @abhishekupadhyay2259
      @abhishekupadhyay2259 3 года назад +1

      @@AyushiSharmaDSA it will be ok if u give the code in Java . Btw Im finally placed in software development engineer in Test in Qualitest ( World largest AI company) . Tysm, the credit goes to u .

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад

      @@abhishekupadhyay2259 thank you Abhishek and congratulations 🎉. But all credit goes to you, it's result of your hardwork and consistency. I'll sure provide code in java :)

  • @anveshasharma2726
    @anveshasharma2726 2 года назад +6

    Great explanation as always di and the English version of your videos are also excellent than Hindi one 😊. Keep growing

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      Thank you Anvesha, glad it was helpful :)

    • @tusharnain6652
      @tusharnain6652 2 года назад +4

      @@AyushiSharmaDSA Hindi's pretty good, stick to that one. 😅

  • @kumaraditya7795
    @kumaraditya7795 Год назад +1

    at 13:57 how is 2-1+1=3 and how come you have taken left index as 1

  • @vidhya821
    @vidhya821 Год назад

    why did the video end abruptly?

  • @ThisIsAman007
    @ThisIsAman007 4 месяца назад

    why it ended abruptly

  • @Star_Bawa9
    @Star_Bawa9 2 года назад +1

    Please share an efficient note making strategy for this ...... I am struggling to make notes should i just use comments or maybe a notebook

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      for each problem, you can write the concept used in the problem :)

  • @mohitpandya_2228
    @mohitpandya_2228 Год назад

    Hey Aayushi! I just came with an aproach I don't know if it is fine correct me out for this one! Lets suppose i have given a string of length n and i reverse the string first. On the next step i find the longest contagious common substring. Isn't it the same thing? Like wont it give me the longest common subsequence? Help me out on this one! Thank you

  • @kundanmane9665
    @kundanmane9665 Год назад

    Nice explanation...please provide java code for this...

  • @tarunbhatt8365
    @tarunbhatt8365 2 года назад +1

    Hi Ayushi,
    Nice explanation, liked it. Just a small query, the above code is not passing for some inputs like abb or baa, can you please check once to try these cases for your code?

  • @sanatangyansagar
    @sanatangyansagar 2 года назад

    Thank you Bhan 🙏🏻❤️

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      Welcome Shubham😊

    • @sanatangyansagar
      @sanatangyansagar 2 года назад

      @@AyushiSharmaDSA i will watch your every video I really like your content 😊

  • @gauravsoni2919
    @gauravsoni2919 Год назад

    mam plz give the code of this in c++ as well

  • @meetsoni1938
    @meetsoni1938 2 года назад

    Very nicely explained 😊

  • @kanchidoshi6907
    @kanchidoshi6907 2 года назад

    Video is cut off. Can you pls post full video?

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад

      Yes, I realized that, I'll post a new one, will remove this. Thanks for letting me know :)

  • @sakshisharma6765
    @sakshisharma6765 Год назад

    Nice explanation :)

  • @LetsPlayMortal
    @LetsPlayMortal 11 месяцев назад

    which app whiteboard used by you?

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  10 месяцев назад +1

      microsoft whiteboard (some old version) :)

  • @GOPINATHBCSE
    @GOPINATHBCSE Год назад

    13:54 2-0+1

  • @hritikrawat6452
    @hritikrawat6452 2 года назад

    *2-0+1=3.

  • @BrainyiFY_o0
    @BrainyiFY_o0 Год назад +2

    At 13:56
    The index is (r-l+1)
    Why it is so can anyone explain this

  • @kumarshivam3661
    @kumarshivam3661 Год назад

    Shat is time complexity of this question?

  • @neelesh621
    @neelesh621 2 года назад +1

    this video seems as incomplete,as coding part just started and video got over

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +3

      Hi, Yes I just realized , I'm not sure how it happened, earlier it was okay, I'll check it. Thanks for letting me know

    • @neelesh621
      @neelesh621 2 года назад

      plz update the video @Ayushi Sharma , u explain vey well, code will help us to understand even more

    • @navinchainani4721
      @navinchainani4721 Год назад

      @@AyushiSharmaDSA please upate the video

  • @AyarnMan
    @AyarnMan 2 года назад

    Thanks :)

  • @deepakreddy715
    @deepakreddy715 8 месяцев назад

    Write code and explain

  • @viviansharma8702
    @viviansharma8702 3 года назад +2

    Hi mam , I am currently working as a fresher ( exp - 4months ). In which language should I prepare DSA python or Java for getting placement in product based company for a backend developer role. Also can you tell me which backend framework is more useful to learn at present nodejs (express js) or Java (Spring boot) .
    It will be very kind of you If you can please solve my doubt. I am very confused.
    Thanks very much

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +2

      Hi Vivian, do dsa in java and you can learn spring boot since you will be doing dsa in java. But both are equally important in industry, you can choose any between spring boot or node js

    • @viviansharma8702
      @viviansharma8702 3 года назад

      @@AyushiSharmaDSA Thanks very much for reply.
      Also can you please tell me which career is better in terms of Compensation Data Engineer or Backend Developer or Full Stack Developer at a product based company like yours. I am asking so as at present I am working on a Big Data project in my company

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +1

      @@viviansharma8702 Truly, I also don't have much idea on this

    • @viviansharma8702
      @viviansharma8702 3 года назад

      @@AyushiSharmaDSA Ok mam.
      Thanks

  • @igmazahmad668
    @igmazahmad668 Год назад

    chooona laga diya ree

  • @ritikroshanyadav-cf3dq
    @ritikroshanyadav-cf3dq 10 месяцев назад

    Merey saath dhoka hua example 3 and 4 nahi Diya tha merey me 😑😐

  • @praviiiin
    @praviiiin Год назад

    where are you from? Himachal/Punjab/Chandighar? that "chrecter" and "meeddle" accent sounds familiar 💖

  • @cr7johnChan
    @cr7johnChan 2 года назад

    //somehow worked since time complexity was |S|^2
    void check(string s ,int left,int right,string&res){
    while(s[left]==s[right] && left>=0 && right

  • @bharathnagilla1807
    @bharathnagilla1807 3 года назад +1

    Ayushi does companies see what kind of level of coders are we like 5star etc or see in which website have we practice coding pls guide 🙏🙏

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +1

      No, you should have good problem solving skills

    • @bharathnagilla1807
      @bharathnagilla1807 3 года назад

      @@AyushiSharmaDSA thank you for your reply:)
      Which website is best for practice out of gfg interview bit hacerrank and earth etc or codestudio which has all in one

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад +3

      @@bharathnagilla1807 interview bit, gfg, leet code, codestudio all are good

  • @jivanninawe3190
    @jivanninawe3190 3 года назад

    Mam basic problem karav na😁 string ke

  • @fatehpreetsingh1440
    @fatehpreetsingh1440 2 года назад +3

    Great explanation maam(as always)
    maam i had a doubt
    maam if we find the longest common substring of string a and its reverse we must get the longest palindromic substring right?

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  2 года назад +1

      Thank you Fateh :), yesss, we will get Longest palindromic substring

    • @fatehpreetsingh1440
      @fatehpreetsingh1440 2 года назад

      @@AyushiSharmaDSA ma’am it’s not passing all test cases can you please help me out

    • @Ddshotaman
      @Ddshotaman 2 года назад +2

      @@fatehpreetsingh1440 Let’s try another example: SS = "abacdfgdcaba", S'S

      = "abacdgfdcaba".
      The longest common substring between SS and S'S

      is "abacd". Clearly, this is not a valid palindrome.

    • @alexrcrew1975
      @alexrcrew1975 2 года назад +1

      @@Ddshotaman yea i have tried the same solution in leetcode for this problem,its passing for 171 test cases and failing on this one.

    • @alina8023
      @alina8023 Год назад

      @@alexrcrew1975 Same Here

  • @cautioni
    @cautioni Год назад

    i don't wanna be rude, but code k saath explain kr sakte the, code smjhate mei aadhe mei video khtm ho gaya, didn't understand at all!

  • @prathamsonawane9165
    @prathamsonawane9165 3 года назад

    13:53
    2-0+1=3 :P

  • @bharathnagilla1807
    @bharathnagilla1807 3 года назад

    Ayushi could you pls suggest me which practice website is good
    Hacker rank hackerearth codeshef codeforces interview bit leetcode gfg
    And opinions on codestudio by coding ninjas pls

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  3 года назад

      Start leetcode, codestudio is also good. It has structured content when it comes to guided path for dsa Or competitive

  • @SaumyaAwasthi_12
    @SaumyaAwasthi_12 3 года назад

    Great :-)

  • @ecs185_shaileshbharti3
    @ecs185_shaileshbharti3 3 года назад

    Reach ++;

  • @sagardutta799
    @sagardutta799 Год назад

    Behen TLE aaraha.. memoize kia phir bhi

    • @sagardutta799
      @sagardutta799 Год назад

      //{ Driver Code Starts
      //Initial Template for Java
      import java.io.*;
      import java.util.*;
      class GFG
      {
      public static void main(String args[])throws IOException
      {
      BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
      int t = Integer.parseInt(read.readLine());
      while(t-- > 0)
      {
      String S = read.readLine();

      Solution ob = new Solution();
      System.out.println(ob.longestPalin(S));
      }
      }
      }
      // } Driver Code Ends
      //User function Template for Java
      class Solution{
      static String f_odd(int idx, String s, String max, Map memoOdd){
      if(idx==s.length()-1)
      return max;
      if (memoOdd.containsKey(idx + "|" + max))
      return memoOdd.get(idx + "|" + max);
      String ans = s.charAt(idx)+"";
      int i = idx-1, j = idx+1;
      while(0

  • @ayushjain386
    @ayushjain386 2 года назад

    Maja nhi smjne me

  • @itsfunny01
    @itsfunny01 День назад

    hello,im your biggest fan please guide me ,i lost my roadmap.