The time in words solves the problem of writing the time based on a time stamp and putting it into words. Its recommends using dictionary to solve and for this I use a HashMap, which is essentially a dictionary. This creates a one for one relation ship called Key,Value pairings. If you have a key you can map it to a value, this is useful for direct lookups. In Java you will want to use an ArrayList for iterating over every item, but if you are getting one item and you have the key to that item a hash map will greatly increase the time to get the value you are looking for.

https://www.hackerrank.com/challenges/the-time-in-words

import java.util.HashMap;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(0, "o' clock");
hm.put(1, "one");
hm.put(2, "two");
hm.put(3, "three");
hm.put(4, "four");
hm.put(5, "five");
hm.put(6, "six");
hm.put(7, "seven");
hm.put(8, "eight");
hm.put(9, "nine");
hm.put(10, "ten");
hm.put(11, "eleven");
hm.put(12, "twelve");
hm.put(13, "thirteen");
hm.put(14, "fourteen");
hm.put(15, "quarter");
hm.put(16, "sixteen");
hm.put(17, "seventeen");
hm.put(18, "eighteen");
hm.put(19, "nineteen");
hm.put(20, "twenty");
hm.put(21, "twenty one");
hm.put(23, "twenty three");
hm.put(24, "twenty four");
hm.put(25, "twenty five");
hm.put(26, "twenty six");
hm.put(27, "twenty seven");
hm.put(28, "twenty eight");
hm.put(29, "twenty nine");
hm.put(30, "half");
hm.put(31, "thirty one");
hm.put(32, "thirty two");
hm.put(32, "thirty three");
hm.put(34, "thirty four");
hm.put(35, "thirty five");
hm.put(36, "thirty six");
hm.put(37, "thirty seven");
hm.put(38, "thirty eight");
hm.put(39, "thirty nine");
hm.put(40, "fourty");
hm.put(41, "fourty one");
hm.put(42, "fourty two");
hm.put(43, "fourty three");
hm.put(44, "fourty four");
hm.put(45, "quarter");
hm.put(46, "fourty six");
hm.put(47, "fourty seven");
hm.put(48, "fourty eight");
hm.put(49, "fourty nine");
hm.put(50, "fifty");
hm.put(51, "fifty one");
hm.put(52, "fifty two");
hm.put(53, "fifty three");
hm.put(54, "fifty four");
hm.put(55, "fifty five");
hm.put(56, "fifty six");
hm.put(57, "fifty seven");
hm.put(58, "fifty eight");
hm.put(59, "fifty nine");
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
if (1 > hours || hours >= 12) {
in.close();
return;
}
in.nextLine();
int minutes = in.nextInt();
if (0 > minutes || minutes >= 60) {
in.close();
return;
}
String temp = "";
switch (minutes) {
case 0:
temp = "" + hm.get(hours) + " " + hm.get(minutes);
break;
case 1:
temp = "" + hm.get(minutes) + " minute past " + hm.get(hours);
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
temp = "" + hm.get(minutes) + " minutes past " + hm.get(hours);
break;
case 15:
temp = "" + hm.get(minutes) + " past " + hm.get(hours);
break;
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
temp = "" + hm.get(minutes) + " minutes past " + hm.get(hours);
break;
case 30:
temp = "" + hm.get(minutes) + " past " + hm.get(hours);
break;
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
if (hours == 12)
hours = 0;
temp = "" + hm.get(60 - minutes) + " minutes to " + hm.get(hours + 1);
break;
case 45:
if (hours == 12) {
hours = 0;
}
temp = "quarter to " + hm.get(hours + 1);
break;
case 46:
case 47:
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
case 58:
if (hours == 12)
hours = 0;
temp = "" + hm.get(60 - minutes) + " minutes to " + hm.get(hours + 1);
break;
case 59:
if (hours == 12)
hours = 0;
temp = "" + hm.get(60 - minutes) + " minute to " + hm.get(hours + 1);
break;
}
System.out.println(temp);
in.close();
}
}