-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1625.cpp
More file actions
32 lines (31 loc) · 894 Bytes
/
1625.cpp
File metadata and controls
32 lines (31 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
void getSmallest(string& s, int a, int startIdx) {
int curr = s[startIdx] - '0';
int min = 9;
int time = 0;
for (int i = 0; i < 11; ++i) {
curr += a;
curr %= 10;
if (curr < min) {
min = curr;
time = i + 1;
}
}
for (int i = startIdx; i < s.size(); i += 2) {
s[i] = (((s[i] - '0') + a * time) % 10) + '0';
}
}
string findLexSmallestString(string s, int a, int b) {
string res = string(s.size(), '9');
for (int i = 0; i < s.size() + 1; ++i) {
if (b & 1) getSmallest(s, a, 0);
getSmallest(s, a, 1);
if (s < res) {
res = s;
}
s = s.substr(s.size() - b) + s.substr(0, s.size() - b);
}
return res;
}
};