Skip to content

Instantly share code, notes, and snippets.

@varlal
Created June 17, 2016 15:51
Show Gist options
  • Save varlal/8da41437677ee224adf2416a1ff68c1c to your computer and use it in GitHub Desktop.
Save varlal/8da41437677ee224adf2416a1ff68c1c to your computer and use it in GitHub Desktop.
SwappingDigits.java
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