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

preview_player
Показать описание

USE: SCTCJSGXLD TO GET 50% DISCOUNT ON THE REGISTRATION FEES

Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)

#DataStructuresAndAlgorithms
#LoveBabbarDSASheet
#interviewpreparation
Longest Palindromic Substring solution
Longest Palindromic Substring Leetcode
Longest Palindromic Substring C++
Longest Palindromic Substring Java
Longest Palindromic Substring Python

🔥🔥🔥🔥👇👇👇

Checkout the series: 🔥🔥🔥
LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Рекомендации по теме
Комментарии
Автор

USE: SCTCJSGXLD TO GET 50% DISCOUNT ON THE REGISTRATION FEES

AyushiSharmaDSA
Автор

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

kumaraditya
Автор

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<n;i++){

//even case
low = i;
high = i-1;
while(low>=0 && high<n && s[low]==s[high]){

if(high-low+1 > 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<n && s[low]==s[high]){

if(high-low+1 > len){
st = low;
len = high-low+1;
}
low--;high++;
}


}

return s.substr(st, len);

}

arpitrajput
Автор

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?

tarunbhatt
Автор

nice explanation, I really like your dry run approach before code in almost all your uploads. Someone once said "If we cannot solve a problem by ourselves, don;t expect the computer to solve it for us", sounds like simple statement, but very true.

RajeshS-nj
Автор

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

mohitpandya_
Автор

at 14:00 you did 2-1+1 wrong, it should be 2-0+1. Great video BTW

Quick-ClipsU
Автор

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

BrainyiFY_o
Автор

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

bharathnagilla
Автор

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

praviiiin
Автор

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<n;i++)
{
//even
low=i;
high=i+1;
while(low>=0 && high<n &&
{
if(high-low+1>pal_Length)
{
pal_Start=low;
pal_Length=high-low+1;
}
low--;
high++;
}
//odd
low=i;
high=i;
while(low>=0 && high<n &&
{
if(high-low+1>pal_Length)
{
pal_Start=low;
pal_Length=high-low+1;
}
low--;
high++;
}
}


return s.substring(pal_Start, pal_Start+pal_Length);

}
}

Nilesh
Автор

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

tushar-nain
Автор

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

Star_Bawa
Автор

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

viviansharma
Автор

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

anveshasharma
Автор

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

fahad_mihran
Автор

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

maheshwarang
Автор

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

kanchidoshi
Автор

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

abhishekupadhyay
Автор

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

bharathnagilla
join shbcf.ru