LeetCode 2124 | Check if All A's Appears Before All B's | Java | Easy | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 2124 β Check if All A's Appears Before All B's using String Substring Invariant in Java.
In this problem, we are given a string s composed solely of characters 'a' and 'b'. We are tasked with determining whether every 'a' appears strictly before every 'b'. A string satisfies this condition if and only if no 'a' appears after any 'b' throughout the entire string.
Rather than running complex two-pointer traversals or tracking first and last seen indices, we recognize a critical invariant: if any 'a' appears after a 'b', there must exist at least one adjacent pair forming the substring "ba". Consequently, verifying that s does not contain "ba" via !s.contains("ba") provides an optimal, one-line linear time O(N) solution that operates with zero extra memory overhead O(1).
π₯ Problem Highlights
β Substring Invariant: Valid strings never contain the transition "ba"
β Single-pass linear scan checking adjacent character relationships
β Works seamlessly for edge cases: pure 'a's, pure 'b's, or empty transitions
β Clean, idiomatic, and highly readable one-liner implementation
β Instant execution under 1 ms well within N up to 100 constraints
β Optimal O(N) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:38 Problem Understanding
04:36 Observation
07:26 Approach
09:28 Code Explanation
11:27 Code Run
13:23 Time Complexity
14:25 Space Complexity
15:11 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/check-if-all-as-appears-before-all-bs
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
String Processing Algorithms
Two Pointers Patterns
Substring Search Invariants
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is checking if the substring "ba" exists in string s sufficient to prove that not all 'a's appear before all 'b's?
1οΈβ£ Because the string must always be sorted in ascending ASCII order
2οΈβ£ Any occurrence of 'a' after 'b' requires at least one adjacent "ba" transition
3οΈβ£ Because "ba" has length 2 and the string only contains two distinct characters
4οΈβ£ Java's contains method is faster than linear indexing
#LeetCode2124 #LeetCode #CheckIfAllAsAppearsBeforeAllBs #Java #JavaDSA #String #TwoPointers #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 2124 β Check if All A's Appears Before All B's using String Substring Invariant in Java.
In this problem, we are given a string s composed solely of characters 'a' and 'b'. We are tasked with determining whether every 'a' appears strictly before every 'b'. A string satisfies this condition if and only if no 'a' appears after any 'b' throughout the entire string.
Rather than running complex two-pointer traversals or tracking first and last seen indices, we recognize a critical invariant: if any 'a' appears after a 'b', there must exist at least one adjacent pair forming the substring "ba". Consequently, verifying that s does not contain "ba" via !s.contains("ba") provides an optimal, one-line linear time O(N) solution that operates with zero extra memory overhead O(1).
π₯ Problem Highlights
β Substring Invariant: Valid strings never contain the transition "ba"
β Single-pass linear scan checking adjacent character relationships
β Works seamlessly for edge cases: pure 'a's, pure 'b's, or empty transitions
β Clean, idiomatic, and highly readable one-liner implementation
β Instant execution under 1 ms well within N up to 100 constraints
β Optimal O(N) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:38 Problem Understanding
04:36 Observation
07:26 Approach
09:28 Code Explanation
11:27 Code Run
13:23 Time Complexity
14:25 Space Complexity
15:11 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/22-November/2124-Check-if-All-As-Appears-Before-All-Bs/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/check-if-all-as-appears-before-all-bs
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
String Processing Algorithms
Two Pointers Patterns
Substring Search Invariants
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is checking if the substring "ba" exists in string s sufficient to prove that not all 'a's appear before all 'b's?
1οΈβ£ Because the string must always be sorted in ascending ASCII order
2οΈβ£ Any occurrence of 'a' after 'b' requires at least one adjacent "ba" transition
3οΈβ£ Because "ba" has length 2 and the string only contains two distinct characters
4οΈβ£ Java's contains method is faster than linear indexing
#LeetCode2124 #LeetCode #CheckIfAllAsAppearsBeforeAllBs #Java #JavaDSA #String #TwoPointers #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




