import java.util.* ;
import java.lang.* ;
import java.io.* ;
// Problem Description:
// Reorder the digits of a number, in order to
// get the least one which is greater than the input number
class Ideone
{
List
< Character
> decreasingChars
= new ArrayList ( ) ; int firstDecreasing = getDecreasingChars( number, decreasingChars) ;
if ( isGreatestNumber( firstDecreasing) ) {
return "" ;
}
if ( firstDecreasing > 1 ) {
prefix = number.substring ( 0 , firstDecreasing - 1 ) ;
}
StringBuilder resultBuilder = new StringBuilder( prefix) ;
char target = number.charAt ( firstDecreasing - 1 ) ;
char leastGreater = swapLeastGreater( decreasingChars, target) ;
resultBuilder.append ( leastGreater) ;
appendList( resultBuilder, decreasingChars) ;
return resultBuilder.toString ( ) ;
}
private static Boolean isGreatestNumber
( int firstDecreasing
) { return firstDecreasing == 0 ;
}
private static int getDecreasingChars
( String number, List
< Character
> decreasing
) { int firstDecreasing = number.length ( ) - 1 ;
for ( ; firstDecreasing > 0 ; -- firstDecreasing) {
char curChar = number.charAt ( firstDecreasing) ;
char preChar = number.charAt ( firstDecreasing - 1 ) ;
decreasing.add ( curChar) ;
if ( curChar > preChar) {
break ;
}
}
return firstDecreasing;
}
private static char swapLeastGreater( List< Character> chars, char target) {
char finding = '9' ;
while ( it.hasNext ( ) ) {
char value
= ( ( Character ) it.
next ( ) ) .
charValue ( ) ; if ( value > target && value < finding) {
finding = value;
}
}
return finding;
}
private static void appendList( StringBuilder str, List< Character> chars) {
while ( it.hasNext ( ) ) {
char value
= ( ( Character ) it.
next ( ) ) .
charValue ( ) ; str.append ( value) ;
}
}
//////////////////////////////////////////////////////////////
// Test Code Begins
//////////////////////////////////////////////////////////////
String result
= getLeastGreaterNumber
( number
) ; if ( result.equals ( expected) ) {
System .
out .
println ( "Test passed with number " + number
) ; }
else {
System .
out .
println ( "Test FAILED with number " + number
) ; }
}
private static void test1( ) {
test( number, expected) ;
}
private static void test2( ) {
test( number, expected) ;
}
private static void test3( ) {
test( number, expected) ;
}
private static void test4( ) {
test( number, expected) ;
}
private static void test5( ) {
test( number, expected) ;
}
private static void test6( ) {
test( number, expected) ;
}
private static void test7( ) {
String expected
= "111223233" ; test( number, expected) ;
}
private static void test8( ) {
test( number, expected) ;
}
private static void test9( ) {
String expected
= "9123456788" ; test( number, expected) ;
}
public static void main
( String [ ] args
) { test1( ) ;
test2( ) ;
test3( ) ;
test4( ) ;
test5( ) ;
test6( ) ;
test7( ) ;
test8( ) ;
test9( ) ;
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovLyBQcm9ibGVtIERlc2NyaXB0aW9uOiAKLy8gUmVvcmRlciB0aGUgZGlnaXRzIG9mIGEgbnVtYmVyLCBpbiBvcmRlciB0byAKLy8gZ2V0IHRoZSBsZWFzdCBvbmUgd2hpY2ggaXMgZ3JlYXRlciB0aGFuIHRoZSBpbnB1dCBudW1iZXIKCmNsYXNzIElkZW9uZQp7CglwdWJsaWMgc3RhdGljIFN0cmluZyBnZXRMZWFzdEdyZWF0ZXJOdW1iZXIoU3RyaW5nIG51bWJlcikgewoJCUxpc3Q8Q2hhcmFjdGVyPiBkZWNyZWFzaW5nQ2hhcnMgPSBuZXcgQXJyYXlMaXN0KCk7CgkJaW50IGZpcnN0RGVjcmVhc2luZyA9IGdldERlY3JlYXNpbmdDaGFycyhudW1iZXIsIGRlY3JlYXNpbmdDaGFycyk7CgoJCWlmKGlzR3JlYXRlc3ROdW1iZXIoZmlyc3REZWNyZWFzaW5nKSkgewoJCQlyZXR1cm4gIiI7CgkJfQoJCQoJCVN0cmluZyBwcmVmaXggPSAiIjsKCQlpZihmaXJzdERlY3JlYXNpbmcgPiAxKSB7CgkJCXByZWZpeCA9IG51bWJlci5zdWJzdHJpbmcoMCwgZmlyc3REZWNyZWFzaW5nIC0gMSk7CgkJfQoJCQoJCVN0cmluZ0J1aWxkZXIgcmVzdWx0QnVpbGRlciA9IG5ldyBTdHJpbmdCdWlsZGVyKHByZWZpeCk7CgkJCgkJY2hhci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