Interview Question
Developer Program EngineersCountry: Ukraine
Interview Type: Written Test
It is easy if you follow my post in the given link, but it involves number to string conversion.
For the case you mentioned, frame a string like below and call it as pattern
"111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 99999999"
if single digit number
digits are not repeated
else
if pattern.contains(num.toString())
digits are repeated
else
digits are not repeated
test case:
11
22
33
111
222
333
1111
2222
3333
.
.
.
.
888888888 < 987654321 which is the biggest number with repeated digits
all these numbers when converted to string is a sub string in the pattern :-)
time complexity is O(n * m) where 'n' ranges from 0 to 987654321 and 'm' ranges from 0 to 9 (number of digits) considering Contains and ToString operation for every 'n'
space complexity is O(1) as the size of the pattern string is constant
correct me if I'm wrong.
void rem_dup(int min, int max)
{
int i,val,dig;
int Isloop ,num[10] = {0};
for(i = min; i <= max; i++)
{
val = i;
Isloop = 0;
memset(num, 0, sizeof(num));
while(val != 0)
{
dig = val % 10;
val = val / 10;
num[dig]++;
if(num[dig] > 1)
{
Isloop = 1;
break;
}
}
if(!Isloop)
printf("%d\t", i);
}
}
for(int num=98;num<=104;num++)
{
int n = num;
List<Integer>list=new ArrayList<Integer>();
int rev=0,rmd;
while(n > 0)
{
rmd = n % 10;
list.add(rmd);
rev = rev * 10 + rmd;
n = n / 10;
}
int count=0;
for(int j=0;j<list.size();j++)
{
for(int k=j+1;k<list.size();k++)
{
if(list.get(j)==list.get(k))
{
count++;
break;
}
}
}
if(count==0)
System.out.println(num);
}
}
- Jackmerius Tacktheritrix October 04, 2013