From 0b6bfd864a52bfd13211611b70942fc4fee526c8 Mon Sep 17 00:00:00 2001 From: Gaurav Saha <72292857+gaurav-312@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:05:17 +0530 Subject: [PATCH] code for longest palindromic substring Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cbbd" Output: "bb" --- .../Longest_Palindromic_Substring.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 algorithms/CPlusPlus/Dynamic-Programming/Longest_Palindromic_Substring.cpp diff --git a/algorithms/CPlusPlus/Dynamic-Programming/Longest_Palindromic_Substring.cpp b/algorithms/CPlusPlus/Dynamic-Programming/Longest_Palindromic_Substring.cpp new file mode 100644 index 00000000..5c006b0f --- /dev/null +++ b/algorithms/CPlusPlus/Dynamic-Programming/Longest_Palindromic_Substring.cpp @@ -0,0 +1,50 @@ +class Solution +{ +public: + string longestPalindrome(string s) + { + int n=s.size(); + + //dp[i][j] will be false if substring s[i..j] is not palindrome, Else true + vector> dp(n, vector (n,false)); + + //All substrings of length 1 are palindromes + for(int i=0; ilenMax) + { + lenMax = k; + start=i; + } + } + } + } + + return s.substr(start, lenMax); //print sub-string from start to lenMax + } +};