Search for a specific permutation from a string

Recently, I was entrusted with something similar in preparation for the interview, and I was 40 minutes. I know that I can translate it with conditional statements, but it has become a bit complicated due to restrictions on the first hour and minutes, which slowed me down. Am I missing something, and is there an easier and faster way to do this? I use to have confidence in my ability to handle strings and permutations in general, but now I'm running out of time for this task, and I wonder if I really adhere to the entry-level job standards for a recent graduate.

Task:

What next early time can be displayed using some permutation of these numbers provided in the string given to the method?

Class decision {public String solution (String S); }

Write a function that, given the non-empty string S in the format "HH: MM", returns a string in the same format, indicating the first time after a given time, which can be represented by rearranging the digits in C. For example, given "11:10", your the function should return "01:11"; for "23:58" the result should be "23:58" (since in this case there is only one possible permutation).

+5
source share
4 answers

The most effective approach is deterministic when you create time combinations based on numbers. Since the time has only 4 digits, the maximum combination is 4! = 24. This algorithm creates a maximum of 24 combinations (recursively), but will have a short circuit and only continue building combinations that have valid times. Thus, the actual number of combinations will usually be less than 24.

Given combinations (maximum 24), they are sorted by time. The next time it's just the next item in the sort, following the given time.

Algorithm:

Input: valid time string T
Exit: next time after input time N

Create an empty set (to save the correct times) S
Create valid combinations of T and add to S
Convert S to L
Sort L
Define the index I in L T
If size (L) == 1 then N = T
else N = element in L with index i + 1 (size of the list of modules)

And the code:

public class NextTime { public static void main(String[] args) { String time = "12:12"; System.out.println("Starting time="+time); System.out.println("Next time="+getNextTime(time)); } static Set<Integer> times = new HashSet<>(); private static String getNextTime(String time) { // Create a list with the 4 digits (strip out the ':') List<String> digits = Arrays.asList(time.split("")).stream().filter(d -> !d.equals(":")) .collect(Collectors.toList()); // Build up the set of maximum 24 times timeCombinations(digits, 0, ""); // Sort the list of times (which are integers representing minutes) List<Integer> sortedTimeList = times.stream().sorted().collect(Collectors.toList()); // Find the next index immediately after the current time int newIndex = 0; if (sortedTimeList.size() > 1) { int currentTime = Integer.valueOf(time.substring(0, 2)) * 60 + Integer.valueOf(time.substring(3, 5)); int listPosition = sortedTimeList.indexOf(currentTime); newIndex = (listPosition + 1) % times.size(); } // Turn the time back into a string String newTimeString = String.format("%02d:%02d", sortedTimeList.get(newIndex)/60, sortedTimeList.get(newIndex)%60); return newTimeString; } private static void timeCombinations(List<String> digits, int depth, String result) { if (depth == 4) { // You have a valid time; Convert time string like 1234 to minutes int minutes = Integer.valueOf(result.substring(0, 2)) * 60 + Integer.valueOf(result.substring(2, 4)); times.add(minutes); } else { for (int d = 0; d < digits.size(); d++) { int c = Integer.valueOf(digits.get(d)).intValue(); // Validation rule for the first digit of the hour if (depth == 0 && c > 2) continue; // Validation rule for the first digit of the minutes if (depth == 2 && c > 5) continue; List<String> digitsClone = new ArrayList<>(digits); String resultClone = new String(result) + digitsClone.get(d); // Validation rule for the complete hour if (depth == 1 && Integer.valueOf(resultClone).intValue() > 23) continue; digitsClone.remove(d); timeCombinations(digitsClone, depth + 1, resultClone); } } } 

}

+1
source
  • Change the minutes (35 β†’ 53) if the minutes are longer when replacing and <60 you have finished.
  • If the minutes no longer look at the next largest hour (ex 11 β†’ 12) modulo 24
  • Make sure you can do the next hour (12) with the numbers indicated
  • If so, create the smallest minute time with the remaining numbers, making sure that the minutes are valid, i.e. <60, otherwise increase the time and try again.
  • If you reach the same hour that you started by turning around, return the same permutation that the answer is missing.

This is my pusedocode that I came across. If you see any errors, feel free to report them. Very interesting question by the way

Here is my solution, I had a lot of fun doing this question

 public class Main { public static String nextTime(String s) { int hours; if (s.charAt(3) < s.charAt(4) && s.charAt(4) < '6') return s.substring(0, 3) + s.charAt(4) + s.charAt(3); hours = Integer.parseInt(s.substring(0, 2)); int tempHours = hours + 1; while (tempHours % 24 != hours) { tempHours %= 24; String h = String.format("%02d", tempHours); String minutes = s; for (int i = 0; i < s.length(); i++) { if (h.charAt(0) == s.charAt(i)) { minutes = s.substring(0, i) + s.substring(i + 1); break; } } for (int i = 0; i < minutes.length(); i++) { if (h.charAt(1) == minutes.charAt(i)) { minutes = minutes.substring(0, i) + minutes.substring(i + 1); break; } } if (minutes.length() > 3) { tempHours++; continue; } for (int i = 0; i < minutes.length(); i++) { if (':' == minutes.charAt(i)) { minutes = minutes.substring(0, i) + minutes.substring(i + 1); break; } } for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != h.charAt(0) && h.charAt(1) != s.charAt(i) && s.charAt(i) != ':') { minutes += s.charAt(i); } } if (minutes.charAt(0) < minutes.charAt(1) && Integer.parseInt(minutes) < 60) { return h + ":" + minutes; } else if (minutes.charAt(1) < '6') { return h + ":" + minutes.charAt(1) + minutes.charAt(0); } tempHours++; } return s; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(nextTime(in.next())); in.close(); } 
+1
source

Maybe I'm missing something obvious, but why not just

  • Start with the time provided.
  • Count +1 minute until you reach the same time.
  • Return the first time that has the same digits as the time.

?

+1
source

You should not sort these 4 digits in ascending order and format them in HH: MM should lead to the expected output?

-1
source

Source: https://habr.com/ru/post/1275427/


All Articles