Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. class Solution {
  2. public String nextClosestTime(String time) {
  3. TreeSet<Character> ts = new TreeSet<>();
  4. char[] s = time.toCharArray();
  5. for (char ch : s) {
  6. if (Character.isDigit(ch)) {
  7. ts.add(ch);
  8. }
  9. }
  10.  
  11. if (ts.size() < 2 || ts.size() > 4) {
  12. return "Not possible / Invalid argument";
  13. }
  14.  
  15. for (int i = s.length-1; i >= 0; i--) {
  16. if (!Character.isDigit(s[i]) || ts.higher(s[i]) == null) {
  17. continue;
  18. }
  19.  
  20. s[i] = ts.higher(s[i]);
  21. for (int j = i+1; j < s.length; j++) {
  22. if (!Character.isDigit(s[j])) continue;
  23. s[j] = ts.first();
  24. }
  25. return new String(s);
  26. }
  27. return new String(s);
  28.  
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement