Given a string s consisting of only the characters 'a' and 'b', return true if every 'a' appears before every 'b' in the string. Otherwise, return false.
Example 1:
Input: s = "aaabbb" Output: true Explanation: The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5. Hence, every 'a' appears before every 'b' and we return true.
Example 2:
Input: s = "abab" Output: false Explanation: There is an 'a' at index 2 and a 'b' at index 1. Hence, not every 'a' appears before every 'b' and we return false.
Example 3:
Input: s = "bbb" Output: true Explanation: There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
Constraints:
1 <= s.length <= 100s[i] is either 'a' or 'b'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to check if all the letter 'A's come before all the letter 'B's in a sequence. The brute force method involves looking at each 'A' and 'B' and comparing their positions to see if any 'B's come before any 'A's.
Here's how the algorithm would work step-by-step:
def check_a_before_b(input_string):
last_a_position = -1
for current_index in range(len(input_string)):
if input_string[current_index] == 'A':
last_a_position = current_index
for current_index in range(len(input_string)):
if input_string[current_index] == 'B':
#If a 'B' is found, check if any 'A' came after it.
if last_a_position > current_index:
return False
return TrueThe core idea is to find the first 'B' in the string. Once we find it, we ensure that there are no 'A's after that point. This avoids the need to check all possible arrangements of 'A's and 'B's.
Here's how the algorithm would work step-by-step:
def check_string(input_string):
first_b_found = False
for char in input_string:
if char == 'B':
# Once first 'B' found, mark it.
first_b_found = True
if first_b_found and char == 'A':
# If we find 'A' after first 'B',
# it violates the condition. return False
# If we reach here, condition is met
# because there's no 'A' after 'B' return True| Case | How to Handle |
|---|---|
| Empty string | Return true immediately as there are no A's or B's to violate the condition. |
| String with only A's | Return true since all A's appear before any (non-existent) B's. |
| String with only B's | Return true since there are no A's to violate the condition. |
| String with one character | Return true since a single 'A' or 'B' trivially satisfies the condition. |
| String with 'B' before 'A' | Return false; this is the main failing condition. |
| String with many alternating A's and B's | Return false; this tests for the need to check multiple occurrences. |
| String with leading/trailing whitespace | Trim whitespace from the string before processing to avoid incorrect results based on whitespace characters. |
| String with characters other than 'A' and 'B' | Reject the input or treat the other characters as invalid, potentially throwing an error or returning false. |