(UVA) Counting Chaos - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2284

The solution below handles the given time by increasing the minutes by one, and the hours, when necessary. Then, for each new time tried, we check whether it is a palindrome or not, ignoring the leading zeroes when appropriate.


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String line = br.readLine();
        int numAttempts = Integer.parseInt(line);
        for (int i = 0; i < numAttempts; i++) {
            line = br.readLine();
            String[] s = line.split(":");
            int hours = Integer.parseInt(s[0]);
            int minutes = Integer.parseInt(s[1]);
            
            StringBuilder h = new StringBuilder();
            StringBuilder m = new StringBuilder();
            boolean palindrome = false;
            while (!palindrome) {
                // increase time
                minutes += 1;
                if (minutes == 60) {
                    minutes = 0;
                    hours += 1;
                    hours %= 24;
                }

                // manipulation of strings
                h = new StringBuilder();
                m = new StringBuilder();
                if (hours != 0) {
                    h.append(hours+""); // ignore all leading zeroes in HH
                    // if hours != 0, consider leading zeroes in MM
                    if (minutes/10 == 0) {
                        m.append("0");
                    }
                }
                m.append(minutes+""); 
                                
                // defining time
                StringBuilder time = new StringBuilder();
                time.append(h.toString());
                time.append(m.toString());
                int j = 0;
                int k = time.length()-1;
                for (; j <= k; j++, k--) { // check if the time is palindrome
                    if (time.toString().charAt(j) != time.toString().charAt(k)) {
                        palindrome = false;
                        break;
                    }
                    palindrome = true;
                }
            }
            
            while (h.length() != 2) { // complete with 0
                h.append("0");
                h = h.reverse();
            }
            if (m.length() == 1) { // complete with 0
                m.append("0");
                m = m.reverse();
            }
            System.out.println(h.toString()+":"+m.toString());
        }
                       
        return;
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução