-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1432
More file actions
55 lines (47 loc) · 1.16 KB
/
1432
File metadata and controls
55 lines (47 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
int maxDiff(int num) {
string s=to_string(num);
char maxReplace=' ';
char minReplace=' ';
char charToReplace=' ';
string maxi="";
string mini="";
if (s[0]!='1'){
minReplace=s[0];
charToReplace='1';
}
else{
charToReplace='0';
for (int i=1;i<s.length();i++){
if (s[i]!='0' && minReplace==' ' && s[i]!='1'){
minReplace=s[i];
break;
}
}
}
for (int i=0;i<s.length();i++){
if (s[i]!='9' && maxReplace==' '){
maxReplace=s[i];
break;
}
}
for (int i=0;i<s.length();i++){
if (s[i]==minReplace){
mini.push_back(charToReplace);
}
else{
mini.push_back(s[i]);
}
if (s[i]==maxReplace){
maxi.push_back('9');
}
else{
maxi.push_back(s[i]);
}
}
return stoi(maxi)-stoi(mini);
}
};
TC:O(N)
SC:O(N)