zoukankan      html  css  js  c++  java
  • [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题一模一样,我先给出一个跟1405题类似的做法,代码如下。这个代码的基本思路是因为同一个字母不能连续出现三次,所以如果当前字母还未被 append 过两次且他更多,那么就 append 当前字母,或者是另一个字母已经被 append 过两次了,也 append 当前这个字母。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public String strWithout3a3b(int a, int b) {
     3         StringBuilder sb = new StringBuilder();
     4         int size = a + b;
     5         int A = 0;
     6         int B = 0;
     7         for (int i = 0; i < size; i++) {
     8             if ((a >= b && A != 2) || (a > 0 && B == 2)) {
     9                 sb.append('a');
    10                 a--;
    11                 A++;
    12                 B = 0;
    13             } else if ((b >= a && B != 2) || (b > 0 && A == 2)) {
    14                 sb.append('b');
    15                 b--;
    16                 B++;
    17                 A = 0;
    18             }
    19         }
    20         return sb.toString();
    21     }
    22 }

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

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public String strWithout3a3b(int a, int b) {
     3         StringBuilder sb = new StringBuilder();
     4         char c1 = 'a';
     5         char c2 = 'b';
     6         // c1是较少的字母,c2是较多的字母
     7         if (a > b) {
     8             c1 = 'b';
     9             c2 = 'a';
    10         }
    11 
    12         while (a > 0 && b > 0) {
    13             sb.append(c1);
    14             sb.append(c2);
    15             a--;
    16             b--;
    17         }
    18 
    19         // c2是较多的字母
    20         char c = c2;
    21         StringBuilder sb2 = new StringBuilder();
    22         int max = Math.max(a, b);
    23         // 因为c1是较少的字母,我们才可以把c2放到c1的前面
    24         if (max >= 2) {
    25             sb2.append(c2);
    26             sb2.append(c2);
    27             max -= 2;
    28         } else if (max > 0) {
    29             sb2.append(c2);
    30             max--;
    31         }
    32 
    33         for (int i = 0; i < sb.length(); i++) {
    34             if (sb.charAt(i) == c) {
    35                 sb2.append(sb.charAt(i));
    36             } else {
    37                 sb2.append(sb.charAt(i));
    38                 if (max > 0) {
    39                     sb2.append(c);
    40                     max--;
    41                 }
    42             }
    43         }
    44         return sb2.toString();
    45     }
    46 }

    相关题目

    984. String Without AAA or BBB

    1405. Longest Happy String

    LeetCode 题目总结 

  • 相关阅读:
    Line of Sight 计算几何基础
    Hash算法详解
    高效mysql的习惯(程序员版本)
    thymeleaf初步使用
    @Transactional注解事务不起作用
    泛型的理解
    Git&GitHun 命令合集
    springboot引入thymeleaf
    springboot静态资源映射
    springboot的配置文件
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14296035.html
Copyright © 2011-2022 走看看