Advertisement
nikunjsoni

681

Jun 27th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string nextClosestTime(string time) {
  4.         set<char> sorted;
  5.         for(auto c:time){
  6.             if(c==':') continue;
  7.             sorted.insert(c);
  8.         }
  9.        
  10.         string res = time;
  11.         for(int i = time.size()-1; i>=0; i--){
  12.             if(time[i] == ':' ) continue;
  13.             auto it = sorted.find(time[i]);
  14.              if(*it != *sorted.rbegin()){// not the largest number
  15.                 it++; // go to the next element
  16.                 res[i] = *it;
  17.                 if((i>=3 && stoi(res.substr(3,2))<60) || (i<2 && stoi(res.substr(0,2))<24))      
  18.                     return res;      
  19.              }
  20.              res[i]=*sorted.begin(); // take the smallest number
  21.         }
  22.         return res;  
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement