There are various ways to improve this if you go with this algorithm. First, you do not have to create an array of characters. You can use String.charAt () to access the specific character of your string.
Secondly, you do not need a list. If you used SortedMultiSet or SortedBag, you can just add things in sorted order. If you write a function that creates a SortedMultiSet from your string, you can simply compare the sets without rebuilding the string.
Note. I don’t know which libraries you are allowed to use (Google and Apache have these types), but you can always “brew your own”.
Also, be sure to use generic types for your types. Just defining ArrayLists is pretty risky, IMHO.
source share