Leetcode Problem 984. String Without AAA or BBB

984. String Without AAA or BBB

Leetcode Solutions

Greedy Approach to Construct String Without 'aaa' or 'bbb'

  1. Initialize an empty string s to build the result.
  2. While there are still letters to append (a > 0 or b > 0): a. If the last two characters of s are not the same, append the most common letter (either 'a' or 'b'). b. If the last two characters of s are the same, append the other letter. c. Decrement the counter of the letter appended.
  3. Return the constructed string s.
UML Thumbnail

Alternating Letters Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...