Skip to main content
Back to problems
Codeforces
Easy
Math
Arrays
Beautiful Year

Find the first year after a given year that has all distinct digits.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given a year yy. Your task is to find the smallest year strictly greater than yy such that all four digits of the year are different.

For example, 1987 is beautiful because its digits are all distinct, while 2013 is not because the digit 0 appears twice? Actually 2013 is beautiful; the condition is simply that no digit repeats.

Return the first year after the given one that satisfies this property.

Input Format

  • A single integer yy — the given year.

Output Format

  • Print one integer: the next beautiful year.

Constraints

  • The year is a positive integer with 4 digits.
  • It is guaranteed that a valid answer exists.

Hints

  • Check years one by one starting from y+1y + 1.
  • For each candidate year, verify whether its digits are all distinct.

Input Format

A single integer yy representing the starting year.

Output Format

Print the smallest year greater than yy whose digits are all different.

Constraints

  • 1000y90001000 \le y \le 9000
  • A solution is guaranteed to exist.
Examples
Sample cases returned by the problem API.

Example 1

Input

1987

Output

2013

Explanation

1988, 1989, ..., 2012 are not beautiful. The next year with all distinct digits is 2013.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.