Timeline
Timeline
2025-10-19
init
BFS
Problem:
DFS enumeration:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | using std::string;using std::unordered_set;using std::queue;class Solution {private: void sadd(string &s, int a) { int i, n; n = s.size(); for (i = 1; i < n; i += 2) { s[i] = ((s[i] - '0') + a) % 10 + '0'; } } void sshift(string &s, int b) { int n = s.size(); b = b % n; string substr = s.substr(n - b, b); s.erase(s.end() - b, s.end()); s.insert(s.begin(), substr.begin(), substr.end()); }public: string findLexSmallestString(string s, int a, int b) { unordered_set<string> visited; queue<string> q; string res = s, cur, t; q.push(s); visited.insert(s); // BFS while (!q.empty()) { cur = q.front(); q.pop(); res = min(res, cur); // Two branches: add or rotate // Add operation t = cur; sadd(t, a); if (!visited.count(t)) { visited.insert(t); q.push(t); } // Rotate operation t = cur; sshift(t, b); if (!visited.count(t)) { visited.insert(t); q.push(t); } } return res; }}; |
