Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1285
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1285
Different Digits
By Ines Kereki, ORT Uruguai
Timelimit: 1
The inhabitants of Nlogonia are very superstitious. One of their beliefs is that street house numbers that have a repeated digit bring bad luck for the residents. Therefore, they would never live in a house which has a street number like 838 or 1004.
The Queen of Nlogonia ordered a new seaside avenue to be built, and wants to assign to the new houses only numbers without repeated digits, to avoid discomfort among her subjects. You have been appointed by Her Majesty to write a program that, given two integers N and M, determines the maximum number of houses that can be assigned street numbers between N and M, inclusive, that do not have repeated digits.
Input
Each test case is described using one line. The line contains two integers N and M, as described above (1 ≤ N ≤ M ≤ 5000).
Output
For each test case output a line with an integer representing the number of street house numbers between N and M, inclusive, with no repeated digits.
Sample Input | Sample Output |
87 104 |
14 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#define true 1
#define false 0
int diffDigits(int a, int b);
int main (void)
{
int a, b;
while (scanf("%d %d", &a, &b) != EOF)
printf("%d\n", diffDigits(a, b));
}
int diffDigits(int a, int b)
{
unsigned short i, num;
unsigned short contador = 0;
for (i = a; i < = b; ++i)
{
num = i;
_Bool visitados[10] = { false };
while (num)
{
if (visitados[num % 10])
break;
visitados[num % 10] = true;
num = num / 10;
}
if (num == 0)
++contador;
}
return contador;
}
Copy The Code &
Try With Live Editor
Input
989 1022
22 25
1234 1234
Output
0
3
1
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < = b; ++i)
#define RFOR(i, b, a) for (int i = b; i >= a; --i)
#define REP(i, N) for (int i = 0; i < N; ++i)
#define MAX 5010
#define pb push_back
#define mp make_pair
using namespace std;
const double pi = acos(-1.0);
const double EPS = 1e-9;
const double INF = 1e50;
struct pt;
typedef pair < pt, pt> line;
typedef vector<pt> polygon;
typedef pair < pt, int> ddi;
typedef pair dd;
int cmp(double a, double b = 0.0)
{
if (fabs(a - b) < EPS)
return 0;
return a > b ? 1 : -1;
}
int resp[MAX];
int main()
{
int a, b, c;
FOR(i, 1, 5000)
{
c = i;
bitset < 10> bits;
bool flag = false;
while (c) {
int d = c % 10;
if (!bits[d])
bits.set(d, 1);
else {
flag = true;
break;
}
c /= 10;
}
if (!flag)
resp[i] = 1;
resp[i] += resp[i - 1];
}
while (scanf("%d %d", &a, &b) != EOF) {
printf("%d\n", resp[b] - resp[a - 1]);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
989 1022
22 25
1234 1234
Output
0
3
1
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static Reader in = new Reader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int N, M, s;
while (in.hasNext()) {
N = in.nextInt();
M = in.nextInt();
s = 0;
for (int i = N; i < = M; i++) {
if(!hasRepeatedChars(String.valueOf(i))){
s++;
}
}
out.println(s);
}
in.close();
out.close();
}
private static boolean hasRepeatedChars(String i) {
return i.matches(".*(.).*\\1.*");
}
static class Reader implements Closeable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public Reader(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
private StringTokenizer getTokenizer() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer;
}
public boolean hasNext() throws IOException {
return getTokenizer() != null;
}
public String next() throws IOException {
return hasNext() ? tokenizer.nextToken() : null;
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String[] nextStringArray(int size) throws IOException {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = next();
}
return array;
}
public int[] nextIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) throws IOException {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
@Override
public void close() throws IOException {
tokenizer = null;
reader.close(>;
}
}
}
Copy The Code &
Try With Live Editor
Input
989 1022
22 25
1234 1234
Output
0
3
1
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")
function createInterval(min = 0, max = min, step = 1) {
step = step >= 1 ? step : 1
const size = Math.ceil((max - min + 1) / step)
return Array.from({ length: size }, (_, i) => min + step * i)
}
/** @param {number} num */
function areAllDistinctDigits(num) {
const digits = [...`${num}`]
const uniqueDigits = [...new Set(digits)].join("")
return uniqueDigits === num.toString()
}
function main() {
const responses = []
const limitsPairs = input.map(limits => limits.split(" ").map(num => Number.parseInt(num, 10)))
for (const [min, max] of limitsPairs) {
if (isNaN(min) || isNaN(max)) break // EOFile Condition Verification
const interval = createInterval(min, max, 1)
const numsWithUniquesDigits = interval.filter(areAllDistinctDigits)
responses.push(numsWithUniquesDigits.length)
}
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
989 1022
22 25
1234 1234
Output
0
3
1
#5 Code Example with Python Programming
Code -
Python Programming
def con(m):
n = str(m)
for i in n:
if n.count(i) != 1: return False
return True
while True:
try:
e = str(input()).split()
a = int(e[0])
b = int(e[1])
q = 0
for i in range(a, b+1):
if con(i): q += 1
print(q)
except EOFError: break
Copy The Code &
Try With Live Editor
Input
989 1022
22 25
1234 1234
Output
0
3
1