Skip to content

Commit 87aff2d

Browse files
committed
Time: 165 ms (75.66%), Space: 74.7 MB (69.91%) - LeetHub
1 parent a28c932 commit 87aff2d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string findLexSmallestString(string s, int a, int b) {
4+
unordered_set<string> seen;
5+
queue<string> q;
6+
string ans = s;
7+
q.push(s);
8+
seen.insert(s);
9+
while (!q.empty()) {
10+
string cur = q.front();
11+
q.pop();
12+
ans = min(ans, cur);
13+
string t = cur;
14+
for (int i = 1; i < t.size(); i += 2)
15+
t[i] = (t[i] - '0' + a) % 10 + '0';
16+
if (!seen.count(t)) {
17+
seen.insert(t);
18+
q.push(t);
19+
}
20+
rotate(cur.begin(), cur.begin() + (cur.size() - b), cur.end());
21+
if (!seen.count(cur)) {
22+
seen.insert(cur);
23+
q.push(cur);
24+
}
25+
}
26+
return ans;
27+
}
28+
};

0 commit comments

Comments
 (0)