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

A palindromic time is a time string that reads the same forwards and backwards. Given a time in 24-hour format (HH:MM), we need to find the next closest palindromic time. For example, "12:21" is palindromic, but "12:34" is not.

The algorithm checks if we can form a palindrome with the current hour by reversing it to get the minutes. If not, we increment the hour and try again.

Algorithm Steps

The solution follows these steps ?

  • Extract the hour and minute from the input time string

  • Reverse the hour digits to get potential palindromic minutes

  • If current minutes are less than reversed hour, use current hour

  • Otherwise, increment hour and use reversed digits as minutes

  • Handle edge case: if hour is 23 and minutes ? 32, return -1

Example

Let us see the implementation to find the next palindromic time ?

def get_next_palindrome_time(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)
    
    # Edge case: 23:32 and beyond have no palindromic solution
    if h == 23 and minute >= 32:
        return "-1"
    
    # If current minute is less than reversed hour, use current hour
    if minute < rev_hour:
        if h < 10:
            hour_formatted = "0" + str(h)
        else:
            hour_formatted = str(h)
        
        if rev_hour < 10:
            return hour_formatted + ":0" + rev_hr_str
        else:
            return hour_formatted + ":" + rev_hr_str
    
    # Otherwise, increment hour and use its reverse as minutes
    else:
        h += 1
        rev_hr_str = str(h)[::-1]
        rev_hour = int(rev_hr_str)
        
        if h < 10:
            hour_formatted = "0" + str(h)
        else:
            hour_formatted = str(h)
        
        if rev_hour < 10:
            return hour_formatted + ":0" + rev_hr_str
        else:
            return hour_formatted + ":" + rev_hr_str

# Test the function
time_input = "22:22"
result = get_next_palindrome_time(time_input)
print(f"Input: {time_input}")
print(f"Next palindromic time: {result}")
Input: 22:22
Next palindromic time: 23:32

How It Works

For input "22:22" ?

  • Hour = 22, Minutes = 22

  • Reversed hour = 22 (same as original)

  • Since minutes (22) equals reversed hour (22), we increment hour to 23

  • Reversed 23 = 32, so result is "23:32"

Additional Examples

# Test with different inputs
test_cases = ["12:34", "10:15", "23:30", "05:20"]

for test_time in test_cases:
    result = get_next_palindrome_time(test_time)
    print(f"{test_time} ? {result}")
12:34 ? 13:31
10:15 ? 12:21
23:30 ? 23:32
05:20 ? 05:50

Conclusion

The algorithm efficiently finds the next palindromic time by reversing hour digits to form minutes. It handles edge cases like times after 23:31 where no palindromic solution exists in 24-hour format.

Updated on: 2026-03-25T09:58:30+05:30

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements