Created
June 17, 2016 15:51
-
-
Save varlal/8da41437677ee224adf2416a1ff68c1c to your computer and use it in GitHub Desktop.
SwappingDigits.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collections; | |
import java.util.TreeSet; | |
public class SwappingDigits { | |
public String minNumber(String num) { | |
TreeSet<String> results = new TreeSet<String>(); | |
results.add(num); | |
int n = num.length(); | |
for (int i = 0; i < n; i++) { | |
for (int j = i+1; j < n; j++) { | |
char[] arr = num.toCharArray(); | |
arr[i]=num.charAt(j); | |
arr[j]=num.charAt(i); | |
if (arr[0]!='0') { | |
results.add(new String(arr)); | |
} | |
} | |
} | |
return Collections.min(results); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment