diff --git a/Longest Palindromic Substring b/Longest Palindromic Substring new file mode 100644 index 0000000..d4758c3 --- /dev/null +++ b/Longest Palindromic Substring @@ -0,0 +1,56 @@ + +/*By Harsh Sinha + +Intuition: A palindrome string can be assumed as if mirror is placed in between the string so all the character + before and after are same. + So there can be two possibility for middle point. + If length is even then middle point divide into equal half + whereas if length of string is odd then one character should + overlap with middle point to divide into two equal half. + So we will use each character as a middle point and expand + left and right to till our left character is equal to right character. + For each middle point we have to check for two cases if length can be even of odd. + + +Time complexity is O(n2); as for each character we are expanding left and right in worst case(if all character are same) + we will traverse all character. +Space complexity is O(1); +*/ + +#include +using namespace std; + + int expandAroundCenter(string s, int left, int right) { + + while (left>= 0 && right < s.size() && s[left] == s[right]) { + left--; + right++; + } + return right - left - 1; + } + +string longestPalindrome(string s) { + + int n=s.size(),start=0,end=0; + for(int i=0;i end - start) { + start = i - (len - 1) / 2; + end = i + len / 2; + } + } + string ans=""; + for(int i=start;i>s; + string ans=palidrome(); +}