Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String nextClosestTime(String time) {
- TreeSet<Character> ts = new TreeSet<>();
- char[] s = time.toCharArray();
- for (char ch : s) {
- if (Character.isDigit(ch)) {
- ts.add(ch);
- }
- }
- if (ts.size() < 2 || ts.size() > 4) {
- return "Not possible / Invalid argument";
- }
- for (int i = s.length-1; i >= 0; i--) {
- if (!Character.isDigit(s[i]) || ts.higher(s[i]) == null) {
- continue;
- }
- s[i] = ts.higher(s[i]);
- for (int j = i+1; j < s.length; j++) {
- if (!Character.isDigit(s[j])) continue;
- s[j] = ts.first();
- }
- return new String(s);
- }
- return new String(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement