You are given a string 'str' of length 'N'.
Your task is to return the longest palindromic substring. If there are multiple strings, return any.
A substring is a contiguous segment of a string.
str = "ababc"
The longest palindromic substring of "ababc" is "aba", since "aba" is a palindrome and it is the longest substring of length 3 which is a palindrome.
There is another palindromic substring of length 3 is "bab". Since starting index of "aba" is less than "bab", so "aba" is the answer.
The first line contains a string 'str'.
Output Format :
The output contains the size of the longest palindromic substring if that substring is the answer, else -1.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Follow up:
Try to solve it using O(1) space complexity.
abccbc
bccb
For string "abccbc", there are several palindromic substrings such as "a", "b", "c", "cc", "bccb", and "cbc". However, the longest palindromic substring is "bccb".
aeiou
a
1 <= |str| <= 10^3
Time Limit: 1 sec
Think of brute force and try to generate all substring.
O(N ^ 3), where ‘N’ is the length of the given string.
We are creating every substring which takes N ^ 2 time. Checking whether the substring is palindromic takes O(length of substring) time, which can be maximum O(N). Thus, the total time complexity is O(N ^ 3).
O(N), where ‘N’ is the length of the given string.
We are storing substring in a variable string.