# Java short solution beating 87%

• basic idea is like next permutation
not sure if test cases so far are sufficient, welcome to challenge this.

``````public String nextClosestTime(String time) {
int[] all = new int[4];
all[0] = Integer.parseInt(time.substring(0, 1));
all[1] = Integer.parseInt(time.substring(1, 2));
all[2] = Integer.parseInt(time.substring(3, 4));
all[3] = Integer.parseInt(time.substring(4, 5));
int min = all[0];
Set<Integer> set = new HashSet<>();
for(int i : all) {
min = Math.min(min,i);
}

for(int i = 3;i>=0;i--){
int x = all[i];
for(int j = x+1;j<=9;j++){
if(set.contains(j)){
String s = res(all, i, j, min);
if(s != null) {
return s;
}
}
}
}
return ""+min+min+":"+min+min;
}

private String res (int[] all, int i, int j, int min){
int temp = all[i];
all[i] = j;
if(all[0]*10+all[1]>=24 || all[2]*10+all[3]>=60){
all[i] = temp;
return null;
}
for(int k = i+1;k<4;k++){
all[k] = min;
}
return ""+all[0]+all[1]+":"+all[2]+all[3];
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.