亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode] String Compression

Cruise_Chan / 2835人閱讀

String Compression

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3.

If the "compressed" string would not become smaller than the original string, your method should return the original string.

You can assume the string has only upper and lower case letters (a-z).

Example

str=aabcccccaaa return a2b1c5a3
str=aabbcc return aabbcc
str=aaaa return a4

Solution
public class Solution {
    /*
     * @param str: a string
     * @return: a compressed string
     */
    public String compress(String str) {
        if (str == null || str.length() < 3) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        Map map = new HashMap<>();
        char pre = str.charAt(0);
        map.put(pre, 1);
        for (int i = 1; i < str.length(); i++) {
            char cur = str.charAt(i);
            if (cur == pre) {
                map.put(pre, map.get(pre)+1);
            } else {
                sb.append(pre);
                sb.append(map.get(pre));
                map.put(pre, 0);
                map.put(cur, 1);
                pre = cur;
            }
        }
        sb.append(pre);
        sb.append(map.get(pre));
        String res = sb.toString();
        return res.length() < str.length() ? res : str;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/68070.html

相關(guān)文章

  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 評論0 收藏0
  • [LintCode] Missing String

    Problem Given two strings, you have to find the missing string. Example Given a string str1 = This is an exampleGiven another string str2 = is example Return [This, an] Solution public class Solution ...

    IamDLY 評論0 收藏0
  • [LintCode] Split String

    Problem Give a string, you can choose to split the string after one character or two adjacent characters, and make the string to be composed of only one character or two characters. Output all possibl...

    Riddler 評論0 收藏0
  • [LintCode] Permutation in String

    Problem Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first strings permutations is the substring of the second string. ...

    wenshi11019 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<