Advertisement

String Without AAA or BBB - LeetCode 984 Solution

String Without AAA or BBB - Complete Solution Guide

String Without AAA or BBB is LeetCode problem 984, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 .

Detailed Explanation

The problem asks us to construct a string `s` of length `a + b` containing exactly `a` 'a' characters and `b` 'b' characters, such that the substrings "aaa" and "bbb" do not appear in `s`. We need to find *any* valid string `s` satisfying these conditions, given the integers `a` and `b`. The constraints specify that 0 <= a, b <= 100 and that a solution is guaranteed to exist.

Solution Approach

The solution uses a greedy approach with a check for consecutive characters. The algorithm iterates while either `a` or `b` is greater than 0. In each iteration, it first checks if the last two characters in the result string are the same. If they are, it means appending the same character again would violate the "aaa" or "bbb" constraint. Therefore, it's *forced* to append the other character. If the last two characters are different, or the string length is less than 2, it greedily appends the character with the higher count (`a` vs. `b`). This process continues until both `a` and `b` become 0, and the resulting string is returned.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty string (or list in Python) to store the result.
  2. Step 2: Enter a `while` loop that continues as long as either `a` or `b` is greater than 0.
  3. Step 3: Inside the loop, check if the length of the result string is at least 2, and if the last two characters of the string are the same.
  4. Step 4: If the last two characters are the same, append the opposite character (if they are 'a', append 'b', and vice versa) and decrement the corresponding count (`a` or `b`).
  5. Step 5: If the last two characters are different (or the string length is less than 2), compare `a` and `b`. Append 'a' to the string if `a > b`, and 'b' if `b >= a`. Decrement the corresponding count.
  6. Step 6: Repeat steps 3-5 until both `a` and `b` are 0.
  7. Step 7: Return the resulting string.

Key Insights

  • Insight 1: The core idea is to use a greedy approach. Prioritize appending the character that appears more frequently (either 'a' or 'b') to maximize the utilization of available characters.
  • Insight 2: To prevent "aaa" or "bbb" from appearing, we must check the last two characters appended to the string. If they are the same, we are forced to append the other character to avoid three consecutive identical characters.
  • Insight 3: The problem guarantees that a solution always exists, implying we don't need to worry about scenarios where we run out of a particular character while being forced to append it.

Complexity Analysis

Time Complexity: O(a+b)

Space Complexity: O(a+b)

Topics

This problem involves: String, Greedy.

Companies

Asked at: Zalando.