[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 }

 

转载于:https://www.cnblogs.com/lz87/p/10357293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值