Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 2124 | Check if All A's Appears Before All B's | Java | Easy | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 2124 | Check if All A's Appears Before All B's | Java | Easy | CodeByTushu"