-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVeci.java
55 lines (42 loc) · 1.71 KB
/
Veci.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Veci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String numberString = scanner.nextLine();
int number = Integer.parseInt(numberString);
List<Integer> visitedDigits = new LinkedList<>();
int currentDigit = 0;
int pos = numberString.length();
boolean possible = false;
while (number != 0) {
currentDigit = number % 10;
visitedDigits.add(currentDigit);
if (found(visitedDigits, currentDigit)) {
possible = true;
break;
}
number /= 10;
pos--;
}
if (!possible) {
System.out.println(0);
return;
}
int digitToSwap = getDigitToSwap(visitedDigits, currentDigit);
visitedDigits.remove((Integer)digitToSwap);
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(numberString.substring(0, pos-1));
sBuilder.append(digitToSwap);
sBuilder.append(String.join("", visitedDigits.stream().sorted().map(e -> String.valueOf(e)).collect(Collectors.toList())));
System.out.println(sBuilder.toString());
}
private static boolean found(List<Integer> visitedDigits, int currentDigit) {
return visitedDigits.stream().filter(e -> e > currentDigit).findAny().isPresent();
}
private static int getDigitToSwap(List<Integer> visitedDigits, int currentDigit) {
return visitedDigits.stream().filter(e -> e > currentDigit).sorted().limit(1).collect(Collectors.toList()).get(0);
}
}