Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create Longest Palindromic Substring.cpp
  • Loading branch information
gantavya1234 authored Oct 1, 2022
commit c17caf35c7345cc6826ce6a4023b676ca8d96687
33 changes: 33 additions & 0 deletions CPP/Problems/Longest Palindromic Substring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
int dp[n][n];

memset(dp,0,sizeof(dp));
int end=1;
int strt=0;

for(int i=0;i<n;i++){
dp[i][i] = 1;
}
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
dp[i][i+1]=1;
strt=i;end=2;
}
}
for(int j=2;j<n;j++){
for(int i=0;i< n-j;i++){
int lft=i;
int rght = i+j;

if(dp[lft+1][rght-1]==1 && s[lft]==s[rght])
{
dp[lft][rght]=1; strt=i; end=j+1;
}
}
}
return s.substr(strt, end);
}
};