![]() |
VOOZH | about |
Given a string you need to find the shortest palindromic substring of the string. If there are multiple answers output the lexicographically smallest.
Examples:
Input: zyzz Output:y Input: abab Output: a
Naive Approach:
Below is the implementation of the above approach:
e
Time Complexity: O(N^2), where N is the length of the string.
Auxiliary Space: O(N^2)
Efficient Approach: An observation here is that a single character is also a palindrome. So, we just need to print the lexicographically smallest character present in the string.
Below is the implementation of the above approach:
e
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.