-4

I have a String s = "103 123 4444 99 2000", and I want to arrange it into "2000 103 123 4444 99". Means the smallest sum of digit will appear on the left, and the largest sum of digit will appear on the right. How to do this?

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116

3 Answers3

1

not quite sure I understand but I'd try something like

  • split the string into an array
  • convert each item of the array into a number as described here
  • sort the arry
  • concatenate the contents of the array back into a string.

But I cant see why 4444 is not at the beginning from what you ask.a

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
1

this should do :

public void reorderIntStrings () {

    //String s = "103 123 4444 99 2000";// --> prints [2000, 103, 123, 4444, 99]
    String s = "2000 10003 1234000 44444444 9999 11 11 22 123";// --> prints [2000, 11, 11, 10003, 22, 123, 1234000, 44444444, 9999]

    //use TreeMap to store the sums as keys. From the javadoc: 'The map is sorted according to the natural ordering of its keys'
    Map<Integer, List<String>> chunks = new TreeMap<> ();

    //split the original string by spaces and loop through the chunks
    for ( String chunk : s.split ( " " ) ) {
        //single chunk's digits sum accumulator
        int sum = 0;
        //loops through the chunk's digits
        for ( int i = 0; i < chunk.length (); i++ ) {
            sum += Integer.parseInt ( "" + chunk.charAt ( i ) );
        }
        //puts the sum as key and the chunk as value in the TreeMap
        //this way when the loop ends the map will be filled with ordered keys and the relative chunks could be printed as needed
        //the map value is a list of to string to collect all the chunks resulting in the same sum
        List<String> list = chunks.get ( sum );
        if ( list == null ) {
            list = new ArrayList<> ();
            chunks.put ( sum, list );
        }
        list.add ( chunk );
    }

    //flattens the results
    List<String> finalList = new ArrayList<> ();
    for ( List<String> v : chunks.values () ) {
        finalList.addAll ( v );
    }

    System.out.println ( finalList );
}
max
  • 783
  • 6
  • 22
  • Your code is working well but you should add some comments into your code. Why did you use "TreeMap()", how do you sort the values(of course TreeMap makes specific guarantees to order)? – Sedat Polat Oct 14 '15 at 17:28
  • Hi Sedat, I added the comments that explain the solution. The TreeMap is a Map implemented as a Red-Black tree that guarantees the keys natural ordering. – max Oct 14 '15 at 18:15
  • this look good, but how about this string: "2000 10003 1234000 44444444 9999 11 11 22 123", it doesn't work well – Twitter khuong291 Oct 15 '15 at 01:35
  • i edited the solution adding a list of strings as the mav value type. this way all the chunks resulting in the same sum can be collected – max Oct 15 '15 at 08:06
0

My solution might be a little overkill...

    import java.util.ArrayList;
    import java.util.Collections;
    public class TestClass {

        public static void main(String[] args) {

            String numberString = "200 100 333 111";
            String[] numberTokens = numberString.split(" ");

            ArrayList<SpecialNumber> list = new ArrayList<SpecialNumber>();
            for (String s : numberTokens) {
                list.add(new SpecialNumber(s));
            }

            Collections.sort(list);
            for (SpecialNumber s : list) {

                System.out.println(s.getNumberString());
            }   
        }
    }
    class SpecialNumber implements Comparable<SpecialNumber>{

        private int sumOfDigits;
        private String numberString;

        public int getSumOfDigits() {
            return this.sumOfDigits;
        }   
        public String getNumberString() {
            return numberString;
        }

        public SpecialNumber(String numberString) {
            this.sumOfDigits = this.countSum(numberString);
            this.numberString = numberString;
        }

        private int countSum(String numberString) {

            int sum = 0;

            for(int i=0; i<numberString.length(); i++) {

                try {
                    Character c_digit = numberString.charAt(i);
                    int digit = Integer.parseInt(c_digit.toString());
                    sum += digit;
                }catch (NumberFormatException nfe) {
                    System.out.println(nfe.toString());
                }
            }
            return sum;
        }

        @Override
        public int compareTo(SpecialNumber o) {
            return this.sumOfDigits - o.sumOfDigits ;
        }
    }
Flexo
  • 1
  • You could avoid the whole `SpecialNumber` stuff if you would use a `Comparator` instead of trying to create a `Comparable` type. – Tom Oct 14 '15 at 17:32
  • Yes you are right. Or should have used Treemap as Max did. Treemap is always ordered by its keys. – Flexo Oct 14 '15 at 17:52