leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1625.cpp (762B)


      1 class Solution {
      2     unordered_set<string> seen;
      3     string res;
      4 
      5     string rotate(string s, int b) {
      6         reverse(s.begin(), s.end());
      7         reverse(s.begin(), s.begin() + b);
      8         reverse(s.begin() + b, s.end());
      9         return s;
     10     }
     11 
     12     string add(string s, int x) {
     13         for (int i = 1; i < s.size(); i += 2)
     14             s[i] = '0' + ((s[i] & 0xF) + x) % 10;
     15         return s;
     16     }
     17 
     18   public:
     19     void dfs(const int a, const int b, const string &s) {
     20         if (seen.count(s)) return;
     21         res = min(res, s);
     22         seen.insert(s);
     23         dfs(a, b, rotate(s, b));
     24         dfs(a, b, add(s, a));
     25     }
     26 
     27     string findLexSmallestString(const string &s, int a, int b) {
     28         res = s;
     29         dfs(a, b, s);
     30         return res;
     31     }
     32 };