How to Sort and Star a String Array in Java


The challenge

You will be given a vector of strings. You must sort it alphabetically (case-sensitive, and based on the ASCII values of the chars) and then return the first value.

The returned value must be a string, and have "***" between each of its letters.

You should not remove or add elements from/to the array.

The solution in Java code

At first we could approach the problem by using the built-in Arrays.sort() method and then looping through the first string, placing asterisks inbetween each letter:

import java.util.*;

public class SortAndStar {

  public static String twoSort(String[] s) {
    Arrays.sort(s);
    StringBuffer sb = new StringBuffer();
    
    int len = s[0].length();
    for (int i=0; i<len; i++) {
      sb.append(s[0].charAt(i));
      if (i<len-1) sb.append("***");
    }
    
    return sb.toString();
  }
}

A cleaner option would be to simply use the String’s join and split built-ins to do this work for us!

public class SortAndStar {
    public static String twoSort(String[] s) {
        java.util.Arrays.sort(s);

        return String.join("***",s[0].split(""));
    }
}

Alternatively, we could also chain our workload and use streams and functional programming to achieve the same thing:

import java.util.Arrays;
public class SortAndStar {

  public static String twoSort(String[] s) {
    return String.join("***", Arrays.stream(s).sorted().findFirst().orElse("").split(""));
  }
}

Test cases to validate our Java code

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.*;
import java.util.stream.*;

public class SolutionTest {
 
    @Test
    public void testFixed() {
        assertEquals("b***i***t***c***o***i***n", SortAndStar.twoSort(new String[] {"bitcoin", "take", "over", "the", "world", "maybe", "who", "knows", "perhaps"}));
        assertEquals("a***r***e", SortAndStar.twoSort(new String[] {"turns", "out", "random", "test", "cases", "are", "easier", "than", "writing", "out", "basic", "ones"}));
        assertEquals("a***b***o***u***t", SortAndStar.twoSort(new String[] {"lets", "talk", "about", "javascript", "the", "best", "language"}));
        assertEquals("c***o***d***e", SortAndStar.twoSort(new String[] {"i", "want", "to", "travel", "the", "world", "writing", "code", "one", "day"}));
        assertEquals("L***e***t***s", SortAndStar.twoSort(new String[] {"Lets", "all", "go", "on", "holiday", "somewhere", "very", "cold"}));
    }
    
    @Test
    public void testRandom() {
      Random random = new Random();
      
      for(int i = 0; i < 200; i++){
        int count = random.nextInt(100) + 1;  
        
        String[] test = new String[count];
        for(int j = 0; j < count; j++) {
          String testString = RandomStringUtils.randomAlphabetic(3, 10);
          test[j] = testString;
        }
        
        assertEquals(SortAndStarSolution.twoSort(test), SortAndStar.twoSort(test));
      }
    }
}

class SortAndStarSolution {
  public static String twoSort(String[] s) {
    Arrays.sort(s);
    return s[0].chars()
      .mapToObj(value -> String.valueOf((char) value))
      .collect(Collectors.joining("***"));
  }
}