Skip to content

Commit 7e42900

Browse files
committed
2124. Check if All A's Appears Before All B's: AC
1 parent 383ccd4 commit 7e42900

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,4 @@ mod s2119_a_number_after_a_double_reversal;
16031603
mod s2120_execution_of_all_suffix_instructions_staying_in_a_grid;
16041604
mod s2121_intervals_between_identical_elements;
16051605
mod s2122_recover_the_original_array;
1606+
mod s2124_check_if_all_as_appears_before_all_bs;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* [2124] Check if All A's Appears Before All B's
3+
*
4+
* 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.
5+
*
6+
* Example 1:
7+
*
8+
* Input: s = "aaabbb"
9+
* Output: true
10+
* Explanation:
11+
* The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
12+
* Hence, every 'a' appears before every 'b' and we return true.
13+
*
14+
* Example 2:
15+
*
16+
* Input: s = "abab"
17+
* Output: false
18+
* Explanation:
19+
* There is an 'a' at index 2 and a 'b' at index 1.
20+
* Hence, not every 'a' appears before every 'b' and we return false.
21+
*
22+
* Example 3:
23+
*
24+
* Input: s = "bbb"
25+
* Output: true
26+
* Explanation:
27+
* There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
28+
*
29+
*
30+
* Constraints:
31+
*
32+
* 1 <= s.length <= 100
33+
* s[i] is either 'a' or 'b'.
34+
*
35+
*/
36+
pub struct Solution {}
37+
38+
// problem: https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/
39+
// discuss: https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/discuss/?currentPage=1&orderBy=most_votes&query=
40+
41+
// submission codes start here
42+
43+
impl Solution {
44+
pub fn check_string(s: String) -> bool {
45+
!s.chars().skip_while(|&x| x == 'a').any(|x| x == 'a')
46+
}
47+
}
48+
49+
// submission codes end
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
55+
#[test]
56+
fn test_2124_example_1() {
57+
let s = "aaabbb".to_string();
58+
59+
let result = true;
60+
61+
assert_eq!(Solution::check_string(s), result);
62+
}
63+
64+
#[test]
65+
fn test_2124_example_2() {
66+
let s = "abab".to_string();
67+
68+
let result = false;
69+
70+
assert_eq!(Solution::check_string(s), result);
71+
}
72+
73+
#[test]
74+
fn test_2124_example_3() {
75+
let s = "bbb".to_string();
76+
77+
let result = true;
78+
79+
assert_eq!(Solution::check_string(s), result);
80+
}
81+
}

0 commit comments

Comments
 (0)