Skip to content

Commit 6de2c85

Browse files
authored
Merge pull request #4012 from ImmidiSivani/leetcode-984
solution added to 984
2 parents b2640c8 + 68f0bfd commit 6de2c85

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
id: string-without-aaa-or-bbb
3+
title: String Without AAA or BBB
4+
sidebar_label: 984-String Without AAA or BBB
5+
tags:
6+
- Greedy
7+
- String
8+
- LeetCode
9+
- Java
10+
- Python
11+
- C++
12+
description: "This is a solution to the String Without AAA or BBB problem on LeetCode."
13+
sidebar_position: 3
14+
---
15+
16+
## Problem Description
17+
18+
Given two integers `a` and `b`, return any string `s` such that:
19+
20+
- `s` has length `a + b` and contains exactly `a` 'a' letters and exactly `b` 'b' letters.
21+
- The substring 'aaa' does not occur in `s`.
22+
- The substring 'bbb' does not occur in `s`.
23+
24+
### Examples
25+
26+
**Example 1:**
27+
28+
```
29+
Input: a = 1, b = 2
30+
Output: "abb"
31+
Explanation: "abb", "bab", and "bba" are all correct answers.
32+
```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: a = 4, b = 1
38+
Output: "aabaa"
39+
```
40+
41+
### Constraints
42+
43+
- `0 <= a, b <= 100`
44+
- It is guaranteed that such a string `s` exists for the given `a` and `b`.
45+
46+
---
47+
48+
## Solution for String Without AAA or BBB Problem
49+
50+
To solve this problem, we need to construct a string of length `a + b` containing exactly `a` 'a' letters and `b` 'b' letters. The key is to ensure that no substring 'aaa' or 'bbb' is formed.
51+
52+
### Approach
53+
54+
1. **Greedy Construction:**
55+
- Start by determining the majority character (the one with the higher count).
56+
- Add two of the majority character if more than one remains, followed by one of the minority character.
57+
- If both characters have equal counts, alternate between the two to avoid 'aaa' or 'bbb'.
58+
59+
2. **Ensure No Consecutive Repetitions:**
60+
- Keep track of the previous two characters to ensure that adding another of the same character won't create three consecutive identical characters.
61+
62+
### Code in Different Languages
63+
64+
<Tabs>
65+
<TabItem value="C++" label="C++" default>
66+
<SolutionAuthor name="@ImmidiSivani"/>
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
string strWithout3a3b(int a, int b) {
72+
string result;
73+
while (a > 0 || b > 0) {
74+
bool writeA = false;
75+
int n = result.size();
76+
if (n >= 2 && result[n - 1] == result[n - 2]) {
77+
if (result[n - 1] == 'b') {
78+
writeA = true;
79+
}
80+
} else {
81+
if (a >= b) {
82+
writeA = true;
83+
}
84+
}
85+
86+
if (writeA) {
87+
result += 'a';
88+
--a;
89+
} else {
90+
result += 'b';
91+
--b;
92+
}
93+
}
94+
return result;
95+
}
96+
};
97+
```
98+
99+
</TabItem>
100+
<TabItem value="Java" label="Java">
101+
<SolutionAuthor name="@ImmidiSivani"/>
102+
103+
```java
104+
class Solution {
105+
public String strWithout3a3b(int a, int b) {
106+
StringBuilder result = new StringBuilder();
107+
while (a > 0 || b > 0) {
108+
boolean writeA = false;
109+
int n = result.length();
110+
if (n >= 2 && result.charAt(n - 1) == result.charAt(n - 2)) {
111+
if (result.charAt(n - 1) == 'b') {
112+
writeA = true;
113+
}
114+
} else {
115+
if (a >= b) {
116+
writeA = true;
117+
}
118+
}
119+
120+
if (writeA) {
121+
result.append('a');
122+
--a;
123+
} else {
124+
result.append('b');
125+
--b;
126+
}
127+
}
128+
return result.toString();
129+
}
130+
}
131+
```
132+
133+
</TabItem>
134+
<TabItem value="Python" label="Python">
135+
<SolutionAuthor name="@ImmidiSivani"/>
136+
137+
```python
138+
class Solution:
139+
def strWithout3a3b(self, a: int, b: int) -> str:
140+
result = []
141+
while a > 0 or b > 0:
142+
writeA = False
143+
if len(result) >= 2 and result[-1] == result[-2]:
144+
if result[-1] == 'b':
145+
writeA = True
146+
else:
147+
if a >= b:
148+
writeA = True
149+
150+
if writeA:
151+
result.append('a')
152+
a -= 1
153+
else:
154+
result.append('b')
155+
b -= 1
156+
157+
return ''.join(result)
158+
```
159+
160+
</TabItem>
161+
</Tabs>
162+
163+
#### Complexity Analysis
164+
165+
- **Time Complexity**: $O(a + b)$, where `a` and `b` are the counts of 'a' and 'b' respectively. We iterate through each character once.
166+
- **Space Complexity**: $O(a + b)$, for storing the result string.
167+
168+
---
169+
170+
<h2>Authors:</h2>
171+
172+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
173+
{['ImmidiSivani'].map(username => (
174+
<Author key={username} username={username} />
175+
))}
176+
</div>
177+
```

0 commit comments

Comments
 (0)