Advertisement
jibha

Untitled

Feb 4th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4. inline string rotate(string s,int b){
  5. return s.substr(s.size()-b,b)+s.substr(0,s.size()-b);
  6. }
  7.  
  8. string add(string s,int a){
  9. string ret;
  10. for(int i=0;i<s.size();i++){
  11. if(i%2==1){
  12. ret+=(char)((s[i]+a-'0')%10+'0');
  13. }else{
  14. ret+=s[i];
  15. }
  16.  
  17. }
  18. return ret;
  19. }
  20.  
  21. void bfs(string s,unordered_map<string,int>& visited, int a, int b){
  22.  
  23. queue<string> q;
  24. q.push(s);
  25.  
  26. while(!q.empty()){
  27.  
  28. int size=q.size();
  29. for(int i=0;i<size;i++){
  30.  
  31. string str=q.front();
  32. q.pop();
  33.  
  34. visited[str]=1;
  35. string added=add(str,a);
  36. string rotated=rotate(str,b);
  37. if(visited[rotated]!=1){
  38. q.push(rotated);
  39. }
  40. if(visited[added]!=1){
  41. q.push(added);
  42. }
  43.  
  44. }
  45. }
  46.  
  47. return;
  48.  
  49. }
  50.  
  51. string findLexSmallestString(string s, int a, int b) {
  52.  
  53. unordered_map<string,int> visited;
  54.  
  55. bfs(s,visited,a,b);
  56.  
  57. // for(auto iter:visited){
  58. // cout<<iter.first<<' ';
  59. // }
  60.  
  61.  
  62. return visited.begin()->first;
  63. }
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement