You are given a number 'N' in the form of a string 'S', which is a palindrome. You need to find the greatest number strictly less than 'N' which is also a palindrome.
Note:1. A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward, such as madam, racecar, 1234321, etc.
2. The numerical value of the given string S will be greater than 0.
3. A single-digit number is also considered a palindrome.
4. The answer number should not contain any leading zeros, except for the case when the answer is 0.
5. Note that the length of the string is nothing but the number of digits in N.
The first line of the input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a string 'S', denoting the number whose next smaller palindrome is to be found.
Output Format:
The only line of output of each test case should contain a string, denoting the next smaller palindrome of 'S'.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= |S| <= 10^4
Where |S| donates the size of string 'S'.
Time Limit: 1sec
1
12321
12221
The next smaller palindrome to 12321 is 12221, as it is strictly less than 12321 and it reads the same from the front and back both.
2
11
101
9
99
O(N), where ‘N’ is the length of the given palindrome.
As we are iterating once through the string, hence the overall time complexity will be O(N).
O(1)
Constant extra space is used.