Check if All A's Appears Before All B's

🏠 ⬅️ ➡️

Given a string s consisting of only the characters 'a' and 'b', return true if every 'a' appears before every 'b' in the string. Otherwise, return false.

Example 1:

Input: s = "aaabbb " Output: true Explanation: The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5. Hence, every 'a' appears before every 'b' and we return true.

Example 2:

Input: s = "abab " Output: false Explanation: There is an 'a' at index 2 and a 'b' at index 1. Hence, not every 'a' appears before every 'b' and we return false.

Example 3:

Input: s = "bbb " Output: true Explanation: There are no 'a's, hence, every 'a' appears before every 'b' and we return true.

Constraints:

  • 1 <= s.length <= 100
  • s[i] is either 'a' or 'b'.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=100) :: s
    logical :: result

    ! Example 1
    s = "aaabbb"
    result = check_order(s)
    if (result) then
        print *, "Every 'a' appears before every 'b' in the string."
    else
        print *, "Not every 'a' appears before every 'b' in the string."
    end if

    ! Example 2
    s = "abab"
    result = check_order(s)
    if (result) then
        print *, "Every 'a' appears before every 'b' in the string."
    else
        print *, "Not every 'a' appears before every 'b' in the string."
    end if

    ! Example 3
    s = "bbb"
    result = check_order(s)
    if (result) then
        print *, "Every 'a' appears before every 'b' in the string."
    else
        print *, "Not every 'a' appears before every 'b' in the string."
    end if

contains

    function check_order(s) result(result)
        implicit none
        character(len=*), intent(in) :: s
        integer :: i, j
        logical :: result

        do i = 1, len(s)-1
            if (s(i:i) == 'a') then
                do j = i+1, len(s)
                    if (s(j:j) == 'a') then
                        exit
                    else if (s(j:j) == 'b') then
                        result = .false.
                        return
                    end if
                end do
            else if (s(i:i) == 'b') then
                do j = i+1, len(s)
                    if (s(j:j) == 'b') then
                        exit
                    else if (s(j:j) == 'a') then
                        result = .false.
                        return
                    end if
                end do
            end if
        end do

        result = .true.
    end function check_order
end program main
Compiled
Executed
Correct
! Declare the input string and a flag to indicate if the condition is met
character(len=*), intent(in) :: s
logical :: is_sorted

! Initialize the flag to false
is_sorted = .false.

! Loop through the string and check if every 'a' appears before every 'b'
do i = 1, len(s)
    if (s(i:i) == 'a') then
        if (i < len(s) .and. s(i+1:i+1) == 'b') then
            exit
        else
            is_sorted = .true.
        end if
    else if (s(i:i) == 'b') then
        exit
    end if
end do

! If the flag is still false, then the condition is not met
if (.not. is_sorted) then
    write (*,*) "false"
else
    write (*,*) "true"
end if

end
🌐 Data from online sources
def firstDayBeenInAllRooms(nextVisit):
    n = len(nextVisit)
    ans = 0
    MOD = 10**9 + 7
    dp = [0] * n

    for i in range(1, n):
        dp[i] = (dp[i - 1] + 1 + dp[i - 1] - dp[nextVisit[i - 1]] + MOD) % MOD
        ans = (ans + dp[i]) % MOD
    return ans

We can solve this problem using dynamic programming. We create an array dp of size n and initialize it with 0's. The dp[i] should represent the number of days it takes to go from room i to room i+1.

Starting from the second room (i=1), we loop through all rooms and update each dp[i] value. To calculate dp[i], we need to perform a cumulative summation of the days it has taken to visit the previous room (dp[i - 1]) plus 1 extra day (day i) and subtract the value at the index stored in nextVisit for the previous room (dp[nextVisit[i - 1]]).

Finally, we add the calculated value modulo 10^9 + 7 to the answer ans. Once we exit the loop, we return ans as the result.

🌐 Data from online sources
int firstDayBeenInAllRooms(vector<int>& nextVisit) {
    int n = nextVisit.size();
    long long ans = 0, MOD = 1e9 + 7;
    vector<long long> dp(n, 0);

    for (int i = 1; i < n; ++i) {
        dp[i] = (dp[i - 1] + 1 + dp[i - 1] - dp[nextVisit[i - 1]] + MOD) % MOD;
        ans = (ans + dp[i]) % MOD;
    }
    return ans;
}

We can solve this problem using dynamic programming. We create an array dp of size n and initialize it with 0's. The dp[i] should represent the number of days it takes to go from room i to room i+1.

Starting from the second room (i=1), we loop through all rooms and update each dp[i] value. To calculate dp[i], we need to perform a cumulative summation of the days it has taken to visit the previous room (dp[i - 1]) plus 1 extra day (day i) and subtract the value at the index stored in nextVisit for the previous room (dp[nextVisit[i - 1]]).

Finally, we add the calculated value modulo 10^9 + 7 to the answer ans. Once we exit the loop, we return ans as the result.