[LeetCode] 984. String Without AAA or BBB

Given two integers a and b, return any string s such that:

  • s has length a + b and contains exactly a 'a' letters, and exactly b 'b' letters,
  • The substring 'aaa' does not occur in s, and
  • The substring 'bbb' does not occur in s.

Example 1:
Input: a = 1, b = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.

Example 2:
Input: a = 4, b = 1
Output: "aabaa"

Constraints:
0 <= a, b <= 100
It is guaranteed such an s exists for the given a and b.

不含 AAA 或 BBB 的字符串。

给定两个整数 A 和 B,返回任意字符串 S,要求满足:

S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母;
子串 'aaa' 没有出现在 S 中;
子串 'bbb' 没有出现在 S 中。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/string-without-aaa-or-bbb
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路一

这道题的大体思想是贪心,但是这道题我会提供两种不同的贪心思路。

这道题算是 1405 题的前置题,因为只涉及两个字母。基本思路是优先放更多的字母,除非这个字母已经连续出现过两次了。

复杂度

时间O(n)
空间O(n)

代码一 - 一般的贪心

Java实现

class Solution {
    public String strWithout3a3b(int a, int b) {
        StringBuilder sb = new StringBuilder();
        int c1 = 0;
        int c2 = 0;
        int n = a + b;
        while (sb.length() < n) {
            if ((a > b && c1 < 2) || (c2 == 2)) {
                sb.append('a');
                c1++;
                c2 = 0;
                a--;
            } else {
                sb.append('b');
                c2++;
                c1 = 0;
                b--;
            }
        }
        return sb.toString();
    }
}

代码二 - 用到优先队列的贪心,对于这道题显得没必要

Java实现

class Solution {
    public String strWithout3a3b(int a, int b) {
        StringBuilder sb = new StringBuilder();
        PriorityQueue<int[]> queue = new PriorityQueue<>((x, y) -> y[1] - x[1]);
        if (a > 0) queue.offer(new int[] { 0, a });
        if (b > 0) queue.offer(new int[] { 1, b });
        while (!queue.isEmpty()) {
            int len = sb.length();
            int[] cur = queue.poll();
            if (len >= 2) {
                if (sb.charAt(len - 1) == (char) ('a' + cur[0]) && sb.charAt(len - 2) == (char) ('a' + cur[0])) {
                    if (queue.isEmpty()) {
                        break;
                    }
                    int[] next = queue.poll();
                    sb.append((char) ('a' + next[0]));
                    if (--next[1] > 0) {
                        queue.offer(next);
                    }
                    queue.offer(cur);
                } else {
                    sb.append((char) ('a' + cur[0]));
                    if (--cur[1] > 0) {
                        queue.offer(cur);
                    }
                }
            } else {
                sb.append((char) ('a' + cur[0]));
				if (--cur[1] > 0) {
					queue.offer(cur);
				}
            }
        }
        return sb.toString();
    }
}

思路二

另一种做法的思想是,因为 A 和 B 这两个字母一定有一个更多,我们可以将 A 和 B 先一一交替放入结果集,类似 ABABAB 这样,然后再考虑把多出来的字母插入 ABABAB 并且保证不要超过同时出现三次这个条件。所以我们先统计一下哪个字母更多,并生成那个 AB 交替的字符串。这里对于交替字符串的生成,我选择先放出现次数少的那个字母,因为这样我才可以将出现次数更多的字母 attach 到这个 StringBuilder 的最前面。如果多的字母还未用完,那么我可以尝试将这个较多的字母再放到每一个较少的字母后面。

复杂度

时间O(n)
空间O(n)

代码

Java实现

class Solution {
    public String strWithout3a3b(int a, int b) {
        StringBuilder sb = new StringBuilder();
        char c1 = 'a';
        char c2 = 'b';
        // c1是较少的字母,c2是较多的字母
        if (a > b) {
            c1 = 'b';
            c2 = 'a';
        }

        while (a > 0 && b > 0) {
            sb.append(c1);
            sb.append(c2);
            a--;
            b--;
        }

        // c2是较多的字母
        char c = c2;
        StringBuilder sb2 = new StringBuilder();
        int max = Math.max(a, b);
        // 因为c1是较少的字母,我们才可以把c2放到c1的前面
        if (max >= 2) {
            sb2.append(c2);
            sb2.append(c2);
            max -= 2;
        } else if (max > 0) {
            sb2.append(c2);
            max--;
        }

        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == c) {
                sb2.append(sb.charAt(i));
            } else {
                sb2.append(sb.charAt(i));
                if (max > 0) {
                    sb2.append(c);
                    max--;
                }
            }
        }
        return sb2.toString();
    }
}

相关题目

984. String Without AAA or BBB
1405. Longest Happy String
posted @ 2021-01-19 01:39  CNoodle  阅读(211)  评论(0)    收藏  举报