Find the time which is palindromic and comes after the given time | Java implementation of the approach ; Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver Code
import java . util . * ; class GFG { static String getTime ( String s , int n ) { String res = " " ; int h = Integer . parseInt ( s . substring ( 0 , 0 + 2 ) ) ; int m = Integer . parseInt ( s . substring ( 3 , 3 + 2 ) ) ; int rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; String rev_hs = Integer . toString ( rev_h ) ; if ( h == 23 && m >= 32 ) { res = " - 1" ; } else if ( m < rev_h ) { String temp = " " ; if ( h < 10 ) temp = "0" ; temp = temp + Integer . toString ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } else { h ++ ; rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; rev_hs = Integer . toString ( rev_h ) ; String temp = " " ; if ( h < 10 ) temp = "0" ; temp = temp + Integer . toString ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } return res ; } public static void main ( String [ ] args ) { String s = "21:12" ; int n = s . length ( ) ; System . out . println ( getTime ( s , n ) ) ; } }