# easy understanding C++ solution with explanation

• subject to `hour < 24` and `minute < 60`.
check if the current digit could be modified, if so, set all digits on its right side to the minimum.

``````class Solution {
public:
string nextClosestTime(string time) {
string s = {time[0], time[1], time[3], time[4]};
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
int m = s.size();
int idx = s.find(time[4]) + 1;
if (idx < m) {
time[4] = s[idx];
return time;
}
idx = s.find(time[3]) + 1;
if (idx < m && s[idx] - '0' < 6) {
time[3] = s[idx];
time[4] = s[0];
return time;
}
idx = s.find(time[1]) + 1;
if (idx < m && (time[0] - '0' < 2 || (time[0] - '0' == 2 && s[idx] - '0' < 4))) {
time[1] = s[idx];
time[3] = time[4] = s[0];
return time;
}
idx = s.find(time[0]) + 1;
if (idx < m && s[idx] - '0' < 3) {
time[0] = s[idx];
time[1] = time[3] = time[4] = s[0];
return time;
}
time[0] = time[1] = time[3] = time[4] = s[0];
return time;
}
};
``````

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