首页 > 代码库 > String 经常用法最优算法实现总结 (二)
String 经常用法最优算法实现总结 (二)
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 = http://www.mamicode.com/null;>
2. List<String> getMostLongDifferent(String str)
说明:
Returns the longest consecutive different substring.
i.e:
("abcabefg") -> "cabefg"
i.e:
("abcabefg") -> "cabefg"
/** * @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; }
3. String normalizeSpace(String str)
说明:
Remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.
/** * 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; } }
String 经常用法最优算法实现总结 (二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。