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;
    • 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"

    Note:

    1. 0 <= A <= 100
    2. 0 <= B <= 100
    3. It is guaranteed such an S exists for the given A and B.
     
    Solution 1. DFS
     
    The naive solution is to use dfs to search one correct answer. 
    1. if counts of a > 0, pick a and search recursively to find an answer. If there is one, return true, else backtrack.
    2. At this point, either counts of a == 0 or picking a does not generate a correct answer. If counts of b > 0, we repeat step 1 by picking b. If there is no correct answer, backtrack.
    3. If we've exhausted both options(either pick a or b) but still do not find a correct answer, it means we must backtrack to previous recursive calls. So return false.
     
    Recurisve call termination condition: First check if there is aaa or bbb when the current string's length >= 3 and return false if there is. Then check if we've used all a and b, if we have, return true.
     
    The runtime of this solution is O(2^(A + B)) in the worst case.  The space complexity is O(A+B) for constructing the result string and A + B possible call stacks.
     1 class Solution {
     2     private String ans = null;
     3     public String strWithout3a3b(int A, int B) {
     4         int[] counts = new int[2];
     5         counts[0] = A;
     6         counts[1] = B;
     7         dfs(counts, new StringBuilder());
     8         return ans;
     9     }
    10     private boolean dfs(int[] counts, StringBuilder sb) {
    11         if(sb.length() >= 3) {
    12             String sub = sb.substring(sb.length() - 3);
    13             if(sub.equals("aaa") || sub.equals("bbb")) {
    14                 return false;
    15             }
    16         }
    17         if(counts[0] == 0 && counts[1] == 0) {
    18             return true;
    19         }
    20         //if there are a available, try pick a first
    21         if(counts[0] > 0) {
    22             sb.append('a');
    23             counts[0]--;
    24             if(dfs(counts, sb)) {
    25                 ans = sb.toString();
    26                 return true;
    27             }
    28             sb.deleteCharAt(sb.length() - 1);
    29             counts[0]++;
    30         }
    31         //if using a does not work, or there are no a, pick b
    32         if(counts[1] > 0) {
    33             sb.append('b');
    34             counts[1]--;
    35             if(dfs(counts, sb)) {
    36                 ans = sb.toString();
    37                 return true;
    38             }
    39             sb.deleteCharAt(sb.length() - 1);
    40             counts[1]++; 
    41         }
    42         //both options are explored with no right answer, return false to backtrack
    43         return false;
    44     }
    45 }

    Solution 2. Greedy 

    Solution 1 is very inefficient in finding any correct answer. It is suited for finding all possible correct answers. We can use the following greedy algorithm.

    At any step, pick the more common character as long as it does not generate subtring aaa or bbb. If the previous two characters are both a, then we have to pick b, vice versa. 

    The runtime is O(A + B) with space complexity of O(A + B) as well. This greedy algorithm is correct since there is at least one correct answer. Even if we loose the problem condition to possibly not have any right answers, this algorithm will still work with a small modification. For a and b,  if max(count(a), count(b)) > (1 + min(count(a), count(b))) * 2,  there would be no right answers.

     1 class Solution {
     2     public String strWithout3a3b(int A, int B) {
     3         StringBuilder sb = new StringBuilder();
     4         while(A > 0 || B > 0) {
     5             if(sb.length() < 2 || sb.charAt(sb.length() - 1) != sb.charAt(sb.length() - 2)) {
     6                 if(A >= B) {
     7                     sb.append('a');
     8                     A--;
     9                 }
    10                 else {
    11                     sb.append('b');
    12                     B--;
    13                 }
    14             }
    15             else if(sb.charAt(sb.length() - 1) == 'a') {
    16                 sb.append('b');
    17                 B--;
    18             }
    19             else {
    20                 sb.append('a');
    21                 A--;
    22             }
    23         }
    24         return sb.toString();
    25     }
    26 }
  • 相关阅读:
    运维:生产日志重复打印了,赶紧来看看~
    就这样,我走过了程序员的前五年。一路风雨泥泞,前方阳光正好。
    一个排序引发的BUG
    曝光一个网站,我周末就耗在上面了。
    我不服!这开源项目居然才888个星!?
    知乎的一次29.7元的咨询
    面试官:啥是请求重放呀?
    414天前,我以为这是编程玄学...
    老爷子这代码,看跪了!
    面试官一个线程池问题把我问懵逼了。
  • 原文地址:https://www.cnblogs.com/lz87/p/10357293.html
Copyright © 2011-2022 走看看