Skip to main content
Back to problems
#2124
Easy Algorithms

Check if all as appears before all bs

String
73.1% acceptance
Feb 25, 2026
846
22
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn check_string(s: String) -> bool {
    // After the first 'b', no 'a' should appear
    !s.as_bytes().windows(2).any(|w| w[0] == b'b' && w[1] == b'a')
  }
}