nikunjsoni

1625

Apr 25th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string findLexSmallestString(string s, int a, int b){
  4.         set<string> st;
  5.         queue<string> q;
  6.         string minstring = s;
  7.         q.push(s);
  8.         st.insert(s);
  9.         while(!q.empty()) {
  10.             string cur = q.front(); q.pop();
  11.             minstring = min(cur, minstring);
  12.             string added  = add(cur,a);
  13.             string rotated = rotate(cur,b);
  14.             if(st.find(added) == st.end())
  15.                 q.push(added), st.insert(added);
  16.             if(st.find(rotated) == st.end())
  17.                 q.push(rotated), st.insert(rotated);
  18.         }
  19.         return minstring;
  20.     }
  21.    
  22.     string add(string s, int x) {
  23.         for (int i=1; i<s.size(); i += 2){
  24.             char &c = s[i];
  25.             c = '0' + (c-'0'+x)%10;
  26.         }
  27.         return s;
  28.     }
  29.    
  30.     string rotate(string s, int x){
  31.         reverse(s.begin(), s.end());
  32.         reverse(s.begin(), s.begin()+x);
  33.         reverse(s.begin()+x, s.end());
  34.         return s;
  35.     }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment