- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the time which is palindromic and comes after the given time in Python

Suppose we have a string s that represents a time in the 24 hours format as HH:MM so that HH will be in range 0 to 23 and MM will be in range 0 to 59, We have to find the next closest time which is a palindrome when read as a string. If there is no such string, then return -1.

So, if the input is like "22:22", then the output will be "23:32".

To solve this, we will follow these steps −

n := size of s

hour_string := substring of s[from index 0 to 2]

minute := substring of s[from index 3 to 5] and convert it to integer

rev_hour := reverse the hour_string and convert it to number

rev_hr_str := reversed of hour_string

h := hour_string as integer

temp := blank string, res := blank string

if h is 23 and minute >= 32, then

res := -1

otherwise when minute < rev_hour , then

if h < 10, then

temp := "0"

temp := temp concatenate h

if rev_hour < 10, then

res := res concatenate temp concatenate ":0" concatenate rev_hr_str

otherwise,

res := res concatenate temp concatenate ":" concatenate rev_hr_str

otherwise,

h := h + 1

rev_hr_str := reversed of h as string

rev_hour := reversed of h

if h < 10, then

temp := "0"

temp := temp concatenate h

if rev_hour < 10, then

res := res concatenate temp concatenate ":0" concatenate rev_hr_str

otherwise,

res := res concatenate temp concatenate ":" concatenate rev_hr_str

return res

## Example

Let us see the following implementation to get better understanding −

def get_next_palindrome_time(s) : n = len(s) hour_string = s[0 : 2] minute = int(s[3 : 5]) rev_hour = int(hour_string[::-1]) rev_hr_str = hour_string[::-1] h = int(hour_string) temp = "" res = "" if (h == 23 and minute >= 32) : res = "-1" elif (minute < rev_hour) : if (h < 10) : temp = "0" temp = temp + str(h) if (rev_hour < 10) : res = res + temp + ":0" + rev_hr_str else : res = res + temp + ":" + rev_hr_str else : h += 1 rev_hr_str = str(h)[::-1] rev_hour = int(rev_hr_str) if (h < 10) : temp = "0" temp = temp + str(h) if (rev_hour < 10) : res = res + temp + ":0" + rev_hr_str else : res = res + temp + ":" + rev_hr_str return res s = "22:22" print(get_next_palindrome_time(s))

## Input

"22:22"

## Output

23:32

- Related Questions & Answers
- Find the minimum time after which one can exchange notes in Python
- Python program to find difference between current time and given time
- Python Pandas - Find the end time for the given Period object
- Python Pandas - Find the start time for the given Period object
- Find a time for which angle between hour and minute hands is given theta in C++
- How to get Time in Milliseconds for the Given date and time in Java?
- Program to find nearest time by reusing same digits of given time in python
- Find the arrangement of queue at given time in C++
- How to access and convert time using the time library in Python
- Find the index which is the last to be reduced to zero after performing a given operation in Python
- Program to find expected growth of virus after time t in Python
- How to remove the notification after the particular time in android?
- What is the difference between compile time errors and run time errors in Java?
- What is the Response Time Testing?
- Find the element before which all the elements are smaller than it, and after which all are greater in Python