Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/CheckIfAllAsAppearsBeforeAllBs2124.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Returns {@code true} if every 'a' in the string appears before every 'b'.
* If any 'a' appears after a 'b', returns {@code false}.
* <p>
* <b>Time Complexity: O(n)</b>
* <ul>
* <li>n = length of the string</li>
* <li>Single pass through the string: O(n)</li>
* </ul>
* <p>
* <b>Space Complexity: O(1)</b>
* <ul>
* <li>Only uses a single boolean flag: O(1)</li>
* </ul>
*
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Loading