Given a string str which stores the time in the 24 hours format as HH:MM such that 0 ≤ HH ≤ 23 and 0 ≤ MM ≤ 59. The task is to find the next closest time which is a palindrome when read as a string. If no such string exists then print -1.
Input: str = “21:12”
The only palindromic time possible in the given hour is 21:12
but it is not greater than the given time so the output will be
the palindromic time in the next hour i.e. 22:22
Input: str = “23:32”
Approach: There are three possible cases:
- If MM < reverse(HH) then the output will be HH as hours and reverse(HH) as minutes.
- If HH = 23 and MM ≥ 32 then output will be -1.
- Else output will be HH + 1 as hours and reverse(HH + 1) as minutes.
Below is the implementation of the above approach:
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Find the minimum time after which one can exchange notes
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Check whether the number can be made palindromic after adding K
- Longest palindromic string possible after removal of a substring
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Minimum minutes needed to make the time palindromic
- Find all distinct palindromic sub-strings of a given string
- Find all palindromic sub-strings of a given string | Set 2
- Find a palindromic string B such that given String A is a subsequense of B
- Find Nth even length palindromic number formed using digits X and Y
- Maximum length palindromic substring such that it starts and ends with given char
- Number of strings which starts and ends with same character after rotations
- Count of vessels completely filled after a given time
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.