Download All Files (as .zip)

CCC 2013 - Problem J3: From 1987 to 2013

I actually wrote this contest and I wrote it in VB. Here is a link to my raw solution.

The problem:

You might be surprised to know that 2013 is the first year since 1987 with distinct digits. The years 2014, 2015, 2016, 2017, 2018, 2019 each have distinct digits. 2012 does not have distinct digits, since the digit 2 is repeated.

Given a year, what is the next year with distinct digits?

Input Specification
The input consists of one integer Y (0 ≤ Y ≤ 10000), representing the starting year

Output Specification
The output will be the single integer D, which is the next year after Y with distinct digits.

Sample Input 1
1987

Output for Sample Input 1
2013

Sample Input 2
999

Output for Sample Input 2
1023

My solution (in Java):

import java.util.Scanner;
import java.io.File;

public class CCC_Problem_J3_From_1987_To_2013 {
    public static void main(String[] args) {
        
        try {
            Scanner file = new Scanner(new File("C:\\Users\\Mike\\Desktop\\ccc_2013\\stage_1\\test_data\\j3\\j3.1.in"));
            int n = Integer.parseInt(file.nextLine());
            n++;
            
            while (!hasDistinctDigits(n)) {
                n++;
            }
            System.out.println(n);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /* returns true if all digits in parameter integer are distinct; false otherwise */
    public static boolean hasDistinctDigits(int n) {
        String s = String.valueOf(n); // string representation of the number
        int[] numbers = new int[10]; // index position represents number, element value represents occurrence of that number
        
        for (int i = 0; i < s.length(); i++) {
            int x = Integer.parseInt(s.substring(i, i+1)); // integer at this part in the string
            numbers[x]++; // increase occurrence of this integer in the array
        }
        
        // check if any digit occurred more than once in the array
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] > 1) { // digit occurred more than once
                return false; // not distinct
            }
        }
        return true; // hasn't returned false yet, so the integer has distinct digits
    }
}
DOWNLOAD as .java

Test cases (as .in files):

Using their test cases:

All the following outputs are exactly the expected outputs.

Input:
3030
Output:
3041
DOWNLOAD as .in

Input:
9029
Output:
9031
DOWNLOAD as .in

Input:
1812
Output:
1820
DOWNLOAD as .in

Input:
1867
Output:
1869
DOWNLOAD as .in

Input:
1776
Output:
1780
DOWNLOAD as .in

Input:
1000
Output:
1023
DOWNLOAD as .in

Input:
2987
Output:
3012
DOWNLOAD as .in

Input:
9
Output:
10
DOWNLOAD as .in

Input:
10
Output:
12
DOWNLOAD as .in

Input:
65
Output:
67
DOWNLOAD as .in

Input:
279
Output:
280
DOWNLOAD as .in

Input:
10000
Output:
10234
DOWNLOAD as .in

Input:
0
Output:
1
DOWNLOAD as .in

Input:
9876
Output:
10234
DOWNLOAD as .in

Input:
987
Output:
1023
DOWNLOAD as .in


           Created: April 18, 2014
Completed in full by: Michael Yaworski