Codeforces 271A Beautiful Year Solution & Explanation

Difficulty : 800

Problem Description

It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits.

Now you are suggested to solve the following problem: given a year number, find the minimum year number which is strictly larger than the given one and has only distinct digits.

Input

The single line contains integer y (1000 ≤ y ≤ 9000) — the year number.

Output

Print a single integer — the minimum year number that is strictly larger than y and all it’s digits are distinct. It is guaranteed that the answer exists.

Eamples

Input1987
Output2013
Input2013
Output2014

Solution

using a while loop to add the year, and check it got distinct digits or not.

C# Solution

Solution1

int year = int.Parse(Console.ReadLine());

while (true)
{
    year++;
    if (year.ToString().Distinct().Count() == year.ToString().Length)
    {
        Console.WriteLine(year);
        break;
    }
}

in C#, we can use LINQ’s Distinct function and Count to check if there any repeat digits.

Java Solution

Solution1

import java.util.Scanner;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year = scanner.nextInt();
        boolean loop = true; 
         
        while (loop) {
            year++;
            
            HashSet<Character> hSet = new HashSet<>();
            char[] digits = Integer.toString(year).toCharArray();
            
            for (char digit : digits) {
                if(!hSet.contains(digit))
                {
                    hSet.add(digit);
                }
            }
            
            if (hSet.size() == digits.length) {
                System.out.println(year);
                loop = false;
            }

        }
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 271A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *