Find the time which is palindromic and comes after the given time
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.
Examples:
Input: str = “21:12”
Output: 22:22
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:22Input: str = “23:32”
Output: -1
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; typedef long long ll; // Function to return the required time string getTime(string s, int n) { // To store the resultant time string res; // Hours are stored in h as integer int h = stoi(s.substr(0, 2)); // Minutes are stored in m as integer int m = stoi(s.substr(3, 2)); // Reverse of h int rev_h = (h % 10) * 10 + ((h % 100) - (h % 10)) / 10; // Reverse of h as a string string rev_hs = to_string(rev_h); if (h == 23 && m >= 32) { res = "-1"; } // If MM < reverse of (HH) else if (m < rev_h) { string temp; // 0 is added if HH < 10 if (h < 10) temp = "0"; temp = temp + to_string(h); // 0 is added if rev_h < 10 if (rev_h < 10) res = res + temp + ":0" + rev_hs; else res = res + temp + ":" + rev_hs; } else { // Increment hours h++; // Reverse of the hour after incrementing 1 rev_h = (h % 10) * 10 + ((h % 100) - (h % 10)) / 10; rev_hs = to_string(rev_h); string temp; // 0 is added if HH < 10 if (h < 10) temp = "0"; temp = temp + to_string(h); // 0 is added if rev_h < 10 if (rev_h < 10) res = res + temp + ":0" + rev_hs; else res = res + temp + ":" + rev_hs; } return res; } // Driver code int main() { string s = "21:12"; int n = s.length(); cout << getTime(s, n); return 0; } |
Python3
# Python3 implementation of the approach # Function to return the required time def getTime(s, n) : # Hours are stored in h as integer h = int(s[0 : 2]); # Minutes are stored in m as integer m = int(s[3 : 5]); # Reverse of h rev_h = (h % 10) * 10 + ((h % 100) - (h % 10)) // 10; # Reverse of h as a string rev_hs = str(rev_h) temp = "" res = "" if (h == 23 and m >= 32) : res = "-1"; # If MM < reverse of (HH) elif (m < rev_h) : # 0 is added if HH < 10 if (h < 10) : temp = "0"; temp = temp + str(h); # 0 is added if rev_h < 10 if (rev_h < 10) : res = res + temp + ":0" + rev_hs; else : res = res + temp + ":" + rev_hs; else : # Increment hours h += 1 # Reverse of the hour after incrementing 1 rev_h = (h % 10) * 10 + ((h % 100) - (h % 10)) //10; rev_hs = str(rev_h); # 0 is added if HH < 10 if (h < 10) : temp = "0"; temp = temp + str(h); # 0 is added if rev_h < 10 if (rev_h < 10) : res = res + temp + ":0" + rev_hs; else : res = res + temp + ":" + rev_hs; return res; # Driver code if __name__ == "__main__" : s = "21:12"; n = len(s); print(getTime(s, n)); # This code is contributed by AnkitRai01 |
PHP
<?php //PHP implementation of the approach // Function to return the required time function getTime( $s, $n) { // To store the resultant time $res=""; // Hours are stored in h as integer $h = intval($s.substr(0, 2)); // Minutes are stored in m as integer $m = intval($s.substr(3, 2)); // Reverse of h $rev_h = ($h % 10) * 10 + (($h % 100) - ($h % 10)) / 10; // Reverse of h as a string $rev_hs = strval($rev_h); if ($h == 23 && $m >= 32) { $res = "-1"; } // If MM < reverse of (HH) else if ($m < $rev_h) { $temp=""; // 0 is added if HH < 10 if ($h < 10) $temp = "0"; $temp = $temp . strval($h); // 0 is added if rev_h < 10 if ($rev_h < 10) $res = $res . $temp . ":0" . $rev_hs; else $res = $res. $temp .":" . $rev_hs; } else { // Increment hours $h++; // Reverse of the hour after incrementing 1 $rev_h = ($h % 10) * 10 + (($h % 100) - ($h % 10)) / 10; $rev_hs = strval($rev_h); $temp=""; // 0 is added if HH < 10 if ($h < 10) $temp = "0"; $temp = $temp . strval($h); // 0 is added if rev_h < 10 if ($rev_h < 10) $res = $res . $temp . ":0" . $rev_hs; else $res = $res . $temp . ":" . $rev_hs; } return $res; } // Driver code $s = "21:12"; $n = strlen($s); echo getTime($s, $n); return 0; // This code is contributed by ChitraNayal ?> |
22:22
Recommended Posts:
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Find the arrangement of queue at given time
- Find the minimum time after which one can exchange notes
- Find time taken for signal to reach all positions in a string
- What to do at the time of Wrong Answer (WA)?
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Time complexity of all permutations of a string
- How to overcome Time Limit Exceed(TLE)?
- Extended Mo's Algorithm with ≈ O(1) time complexity
- Make palindromic string non-palindromic by rearranging its letters
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Case-specific sorting of Strings in O(n) time and O(1) space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



