Skip to content

Palindromic Time

Problem Description

Given a string A which represents a time in 24 hour HH:MM format. Find the minimum number of minutes need to pass to reach palindromic time. Let some time be XY:ZW then it is palindromic if X == W and Y == Z.

Problem Constraints

String A represents a valid time in HH:MM format.
String A represents a valid time in HH:MM format.

Input Format

Given a string A.
Given a string A.

Output Format

Return an integer.
Return an integer.

Example Input

Input 1:
A = "23:59"

Input 2:
A = "21:00"
Input 1:
A = "23:59"

Input 2:
A = "21:00"

Example Output

Output 1:
1
Output 2:
12
Output 1:
1
Output 2:
12

Example Explanation

Explanation 1:
After 1 minute time will be 00:00 which is a palindromic time.

Explanation 2:
After 12 minute time will be 21:12 which is a palindromic time.
Explanation 1:
After 1 minute time will be 00:00 which is a palindromic time.

Explanation 2:
After 12 minute time will be 21:12 which is a palindromic time.

Solution

swift
import Foundation

class Solution {
     
	func solve(_ A: inout String) -> Int {
        
        var ret = 0
        
        let array = A.components(separatedBy:":")
        var hour =  Int(array[0])!
        var minute = Int(array[1])!
    
        var reverseHour = (hour%10)*10+hour/10
        if reverseHour < 60 && reverseHour >= minute {
            return reverseHour - minute
        }
        
        ret += 60 - minute
        minute = 0
        hour += 1
        if hour == 24 {
            hour = 0
        }
        
        reverseHour = (hour%10)*10+hour/10
        while reverseHour >= 60 {
            ret += 60
            hour = hour + 1
            if hour == 24 {
                hour = 0
            }
            reverseHour = (hour%10)*10+hour/10
        }  
        
        ret +=  reverseHour
        return ret     
	}
}
import Foundation

class Solution {
     
	func solve(_ A: inout String) -> Int {
        
        var ret = 0
        
        let array = A.components(separatedBy:":")
        var hour =  Int(array[0])!
        var minute = Int(array[1])!
    
        var reverseHour = (hour%10)*10+hour/10
        if reverseHour < 60 && reverseHour >= minute {
            return reverseHour - minute
        }
        
        ret += 60 - minute
        minute = 0
        hour += 1
        if hour == 24 {
            hour = 0
        }
        
        reverseHour = (hour%10)*10+hour/10
        while reverseHour >= 60 {
            ret += 60
            hour = hour + 1
            if hour == 24 {
                hour = 0
            }
            reverseHour = (hour%10)*10+hour/10
        }  
        
        ret +=  reverseHour
        return ret     
	}
}

References