131 拼接所有的字符串产生字典序最小的字符串
Table of Contents

ac

给定一个字符串的数组strs,请找到一种拼接顺序,使得所有的字符串拼接起来组成的字符串是所有可能性中字典序最小的,并返回这个字符串。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;


public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long)Math.pow(2,29);

    public static void main(String[] args) throws Exception {
       String nStr = reader.readLine();
       int n = Integer.valueOf(nStr);
       List<String> stringList = new ArrayList<>();
       for(int i=0;i<n;i++){
           String tmp = reader.readLine();
           stringList.add(tmp);
       }
       System.out.println(getSmallRs(stringList, n));
    }

    static String getSmallRs(List<String> list, int n){
        StringBuilder sb = new StringBuilder();
        list.sort((a,b)->{
            String s1 = a + b;
            String s2 = b + a;
            return s1.compareTo(s2);
        });
        for(int i=0;i<n;i++){
            sb.append(list.get(i));
        }
        return sb.toString();
    }

}
/*
2
abc
de

abcde

2
b
ba

bba
*/