Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Move from brute-force thinking to an efficient approach using greedy strategy.
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,'aaa' does not occur in s, and'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 <= 100s exists for the given a and b.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
1 2
4 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #984: String Without AAA or BBB
class Solution {
public String strWithout3a3b(int a, int b) {
StringBuilder ans = new StringBuilder();
while (a > 0 && b > 0) {
if (a > b) {
ans.append("aab");
a -= 2;
b -= 1;
} else if (a < b) {
ans.append("bba");
a -= 1;
b -= 2;
} else {
ans.append("ab");
--a;
--b;
}
}
if (a > 0) {
ans.append("a".repeat(a));
}
if (b > 0) {
ans.append("b".repeat(b));
}
return ans.toString();
}
}
// Accepted solution for LeetCode #984: String Without AAA or BBB
func strWithout3a3b(a int, b int) string {
var ans strings.Builder
for a > 0 && b > 0 {
if a > b {
ans.WriteString("aab")
a -= 2
b -= 1
} else if a < b {
ans.WriteString("bba")
a -= 1
b -= 2
} else {
ans.WriteString("ab")
a--
b--
}
}
if a > 0 {
ans.WriteString(strings.Repeat("a", a))
}
if b > 0 {
ans.WriteString(strings.Repeat("b", b))
}
return ans.String()
}
# Accepted solution for LeetCode #984: String Without AAA or BBB
class Solution:
def strWithout3a3b(self, a: int, b: int) -> str:
ans = []
while a and b:
if a > b:
ans.append('aab')
a, b = a - 2, b - 1
elif a < b:
ans.append('bba')
a, b = a - 1, b - 2
else:
ans.append('ab')
a, b = a - 1, b - 1
if a:
ans.append('a' * a)
if b:
ans.append('b' * b)
return ''.join(ans)
// Accepted solution for LeetCode #984: String Without AAA or BBB
struct Solution;
impl Solution {
fn str_without3a3b(mut a: i32, mut b: i32) -> String {
if a == b {
let mut res = "".to_string();
for _ in 0..a {
res += "ab";
}
return res;
}
if a > b {
let mut res = "".to_string();
while a > 0 {
res += "a";
a -= 1;
if a > b {
res += "a";
a -= 1;
}
if b > 0 {
res += "b";
b -= 1;
}
}
return res;
}
if b > a {
let mut res = "".to_string();
while b > 0 {
res += "b";
b -= 1;
if b > a {
res += "b";
b -= 1;
}
if a > 0 {
res += "a";
a -= 1;
}
}
return res;
}
"".to_string()
}
}
#[test]
fn test() {
let a = 1;
let b = 2;
let res = "bab".to_string();
assert_eq!(Solution::str_without3a3b(a, b), res);
}
// Accepted solution for LeetCode #984: String Without AAA or BBB
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #984: String Without AAA or BBB
// class Solution {
// public String strWithout3a3b(int a, int b) {
// StringBuilder ans = new StringBuilder();
// while (a > 0 && b > 0) {
// if (a > b) {
// ans.append("aab");
// a -= 2;
// b -= 1;
// } else if (a < b) {
// ans.append("bba");
// a -= 1;
// b -= 2;
// } else {
// ans.append("ab");
// --a;
// --b;
// }
// }
// if (a > 0) {
// ans.append("a".repeat(a));
// }
// if (b > 0) {
// ans.append("b".repeat(b));
// }
// return ans.toString();
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.