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 = 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<String> getMostLongDifferent(String str)
说明:
Returns the longest consecutive different substring.
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;
		}
    }




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值