nikunjsoni

1881

May 30th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string maxValue(string n, int x) {
  4.         string ans;
  5.         if(n[0] == '-'){
  6.             ans += '-';
  7.             bool added = false;
  8.             for(char ch: n){
  9.                 if(ch == '-') continue;
  10.                 if(ch-'0' > x && !added){
  11.                     ans += ('0'+x);
  12.                     added = true;
  13.                 }
  14.                 ans += ch;
  15.             }
  16.             if(!added)
  17.                 ans += ('0'+x);
  18.         }
  19.         else{
  20.             bool added = false;
  21.             for(char ch: n){
  22.                 if(ch-'0' < x && !added){
  23.                     ans += ('0'+x);
  24.                     added = true;
  25.                 }
  26.                 ans += ch;
  27.             }
  28.             if(!added)
  29.                 ans += ('0'+x);
  30.         }
  31.         return ans;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment