Java 字符串分割及排序
public static String getSmallestAndLargest ( String s , int k ) { String newStr = new String ( s ); java . util . List < String > listStr = new java . util . ArrayList < String >(); for ( int i = 0 ; i < newStr . length ()- 1 ; i ++ ) { if (( newStr . length ()- i ) < k ) { break ; } String str = newStr . substring ( i , i + k ); if (! listStr . contains ( str )) { listStr . add ( str ); } } listStr . sort ( java . util . Comparator . naturalOrder ()); return listStr . get ( 0 )+ "\n" + listStr . get ( listStr . size ()- 1 ); } 示例输入: welcometojava 3 示例输出: ava wel 简单描述就是,把给定的字符串输出下列的格式,有2个要素{第一,每三个字符进行分割。 第二,分割完成后按首字母进行排序} [ava, com, elc, eto, jav, lco, met, oja, ome, toj, wel] 链接:https://www.hackerrank.com/challenges/java-string-c