Leetcode 2124: Check if All A's Appears Before All B's

grid47
grid47
Exploring patterns and algorithms
Apr 8, 2024 3 min read

You are given a string consisting of only the characters ‘a’ and ‘b’. Your task is to determine if all occurrences of ‘a’ appear before all occurrences of ‘b’ in the string. If that is the case, return true; otherwise, return false.
Problem
Approach
Steps
Complexity
Input: You are given a string s consisting of only the characters 'a' and 'b'.
Example: s = 'aaaabbb'
Constraints:
• 1 <= s.length <= 100
• s[i] is either 'a' or 'b'.
Output: Return true if every 'a' appears before every 'b' in the string. Otherwise, return false.
Example: Input: 'aaabbb' -> Output: true
Constraints:
• The input string will contain only 'a' and 'b'.
Goal: Determine if all 'a' characters in the string appear before all 'b' characters.
Steps:
• Check if the substring 'ba' exists in the string.
• If 'ba' is found, return false. Otherwise, return true.
Goal: The string will have a length between 1 and 100, and each character will be either 'a' or 'b'.
Steps:
• The string length is between 1 and 100.
• The string contains only the characters 'a' and 'b'.
Assumptions:
• The string contains only 'a' and 'b'.
• There will be at least one character in the string.
Input: Input: 'aaaabbb'
Explanation: In this case, all the 'a's appear before the 'b's, so we return true.

Input: Input: 'abab'
Explanation: Here, an 'a' appears after a 'b', so we return false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus