-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistinctDigits.cpp
55 lines (50 loc) · 1.31 KB
/
DistinctDigits.cpp
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
#include<bits/stdc++.h>
using namespace std;
void fixUsed(string &ans,int*used){
for(int i = 0; i < 10;i++){
used[i] =0;
}
for(int i=0;i<ans.size();i++){
used[ans[i]-'0']=1;
}
}
int main(){
int l,r;
cin>>l>>r;
string left=to_string(l),right=to_string(r);
string ans="";
int used[10] ={0};
ans+=left[0];
used[left[0]-'0']=1;
for(int i=1;i<left.size();i++){
if(used[left[i]-'0']==0){
ans+=left[i];
used[left[i]-'0']=1;
}
else{
int currR=right[i]-'0';
if(left[i]-'0'>=right[i]-'0'){
if(left[i-1]-'0'<right[i-1]-'0'){
currR=right[i]-'0' +10;
}
}
int idx=left[i]-'0';
while(idx<10 and idx<=currR and used[idx]!=0){
idx++;
}
if(idx>currR or idx==10){
if(ans.size()+1<right.size()){
int temp= stoi(ans);
temp++;
ans=to_string(temp);
fixUsed(ans,used);
}else{
cout<<-1<<endl;
return 0;
}
}
ans+=idx+'0';
}
}
cout<<ans<<endl;
}