【Weekly】No.185

C_01 reformatted string

Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).

You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.

Return the reformatted string or return an empty string if it is impossible to reformat the string.

Input: s = "ab123"
Output: "1a2b3"

Method 1: Simplify the problem

In fact, it is needless to say that it is so complicated. A more complicated way of thinking is,

  • Count characters and character subscripts first.
  • Then by comparing the number of numbers and letters, and finally write a lot of if else to control who executes first.

Simplify the problem:

  • The default number dig is the most. If you wish, the dig is the most through exchange.
  • Then by appending characters with a larger number of append types first, then appending characters with a smaller number of append types.
    • However, a condition needs to be met: abs(dig.size() - alp.size() > 1that is, the difference between the number of the two types of characters is not greater than 1
public String reformat(String s) {
    StringBuilder sb = new StringBuilder();
    ArrayList<Character> dig = new ArrayList<>(), alp = new ArrayList<>();
    char[] S = s.toCharArray();
    for (int i = 0; i < S.length; i++) {
        if (Character.isDigit(S[i])) dig.add(S[i]);
        else alp.add(S[i]);
    }
    if ( Math.abs(dig.size() - alp.size()) > 1)
        return "";

    if (dig.size() < alp.size()) {
        ArrayList<Character> t = dig;
        dig = alp;
        alp = t;
    }
    for (int i = 0; i < dig.size(); i++) {
        sb.append(dig.get(i));
        if (i < alp.size()) {
            sb.append(alp.get(i));
        }
    }
    return sb.toString();
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( n ) O (n)

B_02 None

Method 1: None


B_03 Counting frogs

Give you a string croakOfFrogs, which represents a combination of frog sounds (string "croak") from different frogs Since there can be multiple frogs croaking at the same time, multiple "croaks" are mixed in croakOfFrogs. Please return the minimum number of different frogs required for all frogs in the simulated string.

Note: In order to sound the croak "croak", the frog must output the five letters 'c', 'r', 'o', 'a', 'k' in sequence. If all five letters are not output, then it will not sound.

If the string croakOfFrogs is not a mixture of valid "croak" characters, please return -1.

输入:croakOfFrogs = "crcoakroak"
输出:2 
解释:最少需要两只青蛙,“呱呱” 声用黑体标注
第一只青蛙 "crcoakroak"
第二只青蛙 "crcoakroak"

Method 1: Count

  • Because in a legal string, the number of occurrences of each character must be equal.
  • If there is a different character order, and the number is greater than the characters in the earlier relative order, then this string is illegal.
  • When the number of characters is legal, the number of frogs required depends only on the difference between the number of characters c and k.
    • For example: when a frog calls again, while another frog is also calling, then the number of frogs needed at this time will be 2.
public int minNumberOfFrogs(String croakOfFrogs) {
    int[] c = new int[5];
    char[] s = croakOfFrogs.toCharArray();
    int max = 0;
    for (char ch : s) {
        if (ch == 'c') c[0]++;
        if (ch == 'r') c[1]++;
        if (ch == 'o') c[2]++;
        if (ch == 'a') c[3]++;
        if (ch == 'k') c[4]++;
        for (int i = c.length-1; i >= 1; i--) {
            if (c[i] > c[i-1])
                return -1;
        }
        max = Math.max(max, c[0]-c[4]);
    }
    return c[0] == c[4] ? max : -1;
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( 1 ) O (1)
Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105613074