diff --git a/src/CheckIfAllAsAppearsBeforeAllBs2124.java b/src/CheckIfAllAsAppearsBeforeAllBs2124.java deleted file mode 100644 index b476168..0000000 --- a/src/CheckIfAllAsAppearsBeforeAllBs2124.java +++ /dev/null @@ -1,22 +0,0 @@ -public class CheckIfAllAsAppearsBeforeAllBs2124 { - public boolean checkString(String s) { - boolean isBappear = false; - char[] chars = s.toCharArray(); - for (char c : chars) { - if (c == 'b') { - isBappear = true; - } else { - if (isBappear) { - return false; - } - } - } - return true; - } - - public static void main(String[] args) { - String s = "aaabbb"; - // true - System.out.println(new CheckIfAllAsAppearsBeforeAllBs2124().checkString(s)); - } -} diff --git a/src/main/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124.java b/src/main/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124.java new file mode 100644 index 0000000..6335317 --- /dev/null +++ b/src/main/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124.java @@ -0,0 +1,40 @@ +// Tags: String +package com.leetcode.easy; + +public class CheckIfAllAsAppearsBeforeAllBs2124 { + + /** + * Checks if all 'a' characters in the string appear before all 'b' characters. + *

+ * Returns {@code true} if every 'a' in the string appears before every 'b'. + * If any 'a' appears after a 'b', returns {@code false}. + *

+ * Time Complexity: O(n) + *

+ *

+ * Space Complexity: O(1) + *

+ * + * @param s the string to check, containing only characters 'a' and 'b' + * @return {@code true} if all 'a's appear before all 'b's, {@code false} otherwise + */ + public boolean checkString(String s) { + boolean bSeen = false; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == 'b') { + bSeen = true; + } else { + if (bSeen) { + return false; + } + } + } + return true; + } +} diff --git a/src/test/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124Test.java b/src/test/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124Test.java new file mode 100644 index 0000000..7823bcd --- /dev/null +++ b/src/test/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124Test.java @@ -0,0 +1,37 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CheckIfAllAsAppearsBeforeAllBs2124Test { + private CheckIfAllAsAppearsBeforeAllBs2124 solution; + + @BeforeEach + void setUp() { + solution = new CheckIfAllAsAppearsBeforeAllBs2124(); + } + + @Test + void testCheckString_Example1_AllABeforeB() { + // Input: s = "aaabbb" + // 'a' at indices 0,1,2; 'b' at indices 3,4,5 + assertTrue(solution.checkString("aaabbb")); + } + + @Test + void testCheckString_Example2_AAfterB() { + // Input: s = "abab" + // 'a' at index 2 appears after 'b' at index 1 + assertFalse(solution.checkString("abab")); + } + + @Test + void testCheckString_Example3_NoA() { + // Input: s = "bbb" + // No 'a's, so condition is satisfied + assertTrue(solution.checkString("bbb")); + } +}