Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"aaabbb"
"abab"
"bbb"
minimum-deletions-to-make-string-balanced)check-if-array-is-sorted-and-rotated)check-if-numbers-are-ascending-in-a-sentence)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2124: Check if All A's Appears Before All B's
class Solution {
public boolean checkString(String s) {
return !s.contains("ba");
}
}
// Accepted solution for LeetCode #2124: Check if All A's Appears Before All B's
func checkString(s string) bool {
return !strings.Contains(s, "ba")
}
# Accepted solution for LeetCode #2124: Check if All A's Appears Before All B's
class Solution:
def checkString(self, s: str) -> bool:
return "ba" not in s
// Accepted solution for LeetCode #2124: Check if All A's Appears Before All B's
impl Solution {
pub fn check_string(s: String) -> bool {
!s.contains("ba")
}
}
// Accepted solution for LeetCode #2124: Check if All A's Appears Before All B's
function checkString(s: string): boolean {
return !s.includes('ba');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.