1. String getOrderedString(boolean isDuplicated, String … str)
说明:
Orders all characters in the input strings and return the ordered string.(note: only considering the alphabets and digits) i.e: (false, {"ahcdx","abcuy","cejm"}) ->"abcdehjmuxy" (true, {"ahcdx","abcuy","cejm"}) ->"aabcdehjmuxy"
- /**
- * Orders all characters in the input strings without duplicated str.
- *
- * @Title: getOrderedString
- * @param isDuplicated true/false
- * @param str input str
- * @return ordered string
- */
- public static String getOrderedString(boolean isDuplicated, String... str) {
- if (str == null || str.length == 0) {
- return null;
- }
- // initialize array
- int[] charArrayTmp = new int[MAX_CHAR_LENGTH];
- System.arraycopy(CHARARRAY, 0, charArrayTmp, 0, MAX_CHAR_LENGTH);
- int length = str.length;
- String value = null;
- int valueLength = 0;
- char tempValue;
- int totalCounts = 0;
- // merge and sort the input strs
- for (int i = 0; i < length; i++) {
- value = str[i];
- valueLength = value.length();
- totalCounts += valueLength;
- for (int j = 0; j < valueLength; j++) {
- tempValue = value.charAt(j);
- if (isDuplicated) {
- charArrayTmp[tempValue] += 1;
- } else {
- charArrayTmp[tempValue] = 1;
- }
- }
- }
- char[] newChar = new char[totalCounts];
- int counts = 0;
- int len = 0;
- // append result that has been merged and sorted
- for (int i = MIN_CHAR_LENGTH; i < MAX_CHAR_LENGTH; i++) {
- len = charArrayTmp[i];
- if (len != 0) {
- for (int j = 0; j < len; j++) {
- newChar[counts++] = (char) i;
- }
- }
- }
- return new String(newChar, 0, counts);
- }
2. List
3. String normalizeSpace(String str) 说明:Remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.
- /**
- * @Description: get the longest consecutive different substring.
- * @Title: getMostLongDifferent
- * @param str input string
- * @return the longest consecutive different substring of input
- */
- public static List < String > getMostLongDifferent(String str) {
- if (isEmpty(str)) {
- return null;
- }
- int len = str.length();
- Map < Character,
- Integer > cursor = new HashMap < Character,
- Integer > ();
- cursor.put(str.charAt(0), 0);
- int[] lengthAt = new int[len];
- lengthAt[0] = 1;
- int max = 0;
- for (int i = 1; i < len; i++) {
- char cha = str.charAt(i);
- if (cursor.containsKey(cha)) {
- lengthAt[i] = Math.min(lengthAt[i - 1] + 1, i - cursor.get(cha));
- } else {
- lengthAt[i] = lengthAt[i - 1] + 1;
- }
- max = (max >= lengthAt[i]) ? max: lengthAt[i];
- cursor.put(cha, i);
- }
- List < String > resultList = new ArrayList < String > ();
- for (int i = 0; i < len; i++) {
- if (max == lengthAt[i]) {
- String resultString = str.substring(i - max + 1, i + 1);
- if (!resultList.contains(resultString)) {
- resultList.add(resultString);
- }
- }
- }
- return resultList;
- }
- /**
- * Remove leading and trailing whitespace and then replacing sequences of whitespace characters.
- * by a single space
- *
- * @param str the string need to be normalize space
- * @return string result after normalize space
- */
- public static String normalizeSpace(String str) {
- if (str == null) {
- return null;
- }
- str = str.trim();
- if (str.length() <= 3) {
- return str;
- }
- if (str.indexOf(CommonConstants.DOUBLE_BLANKS, 1) >= 0) {
- char[] chars = str.toCharArray();
- int index = 0;
- for (int i = 0, len = chars.length; i < len; i++) {
- if (chars[i] != CommonConstants.CHAR_BLANKS || chars[i - 1] != CommonConstants.CHAR_BLANKS) {
- chars[index++] = chars[i];
- }
- }
- return new String(chars, 0, index);
- } else {
- return str;
- }
- }
来源: http://www.bubuko.com/infodetail-2068446.html