You have two integers l and r. Find an integer x which satisfies the conditions below:
l≤x≤r.
All digits of x are different.
If there are multiple answers, print any of them.
Input
The first line contains two integers l and r (1≤l≤r≤105).
Output
If an answer exists, print any of them. Otherwise, print −1.
Examples
Input
121 130
Output
123
Input
98766 100000
Output
-1
Note
In the first example, 123 is one of the possible answers. However, 121 can’t be the answer, because there are multiple 1s on different digits.
In the second example, there is no valid answer.
题目思路:本题思路是将l到r之间的每个数都遍历,在遍历的同时判断他们是否符合要求,符合之间输出,不符合输出-1.在遍历一个数的时候,用一个数组的下标存放他们的每一位,并且在存放时此位置加一,如果有下标大于等于2则不符合要求。
ac代码:
#include<stdio.h>
#include
#include
using namespace std;
int main()
{
int l,r,t,flage,dp[15];
scanf("%d%d",&l,&r);
for(int i=l;i<=r;i++)
{
t=i,flage=0;
memset(dp,0,sizeof(dp));
while(t)
{
dp[t%10]++;
if(dp[t%10]>=2)
{
flage=1;
break;
}
t/=10;
}
if(!flage) return !printf("%d\n",i);
}
return !printf("-1\n");
}