诶 | #字符串组合#
字符串组合
https://www.nowcoder.com/practice/837f4d04f5cb4f26a8215b2b95cc76a5
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\n"); while (scanner.hasNext()) { String S = scanner.next(); TreeSet<String> treeSet = new TreeSet<>(); char[] ch = S.toCharArray(); //起点 for (int i = 0; i < ch.length; i++) { //终点-1 for (int j = i + 1; j <= ch.length; j++) { treeSet.add(S.substring(i, j)); } } treeSet.stream().sorted(new Comparator<String>() { @Override public int compare(String o1, String o2) { if (o1.length() < o2.length()) return -1; else if (o1.length() == o2.length()) { return o1.compareTo(o2); } return 1; } }).forEach(o-> { System.out.print(o + " "); }); } } }