We define the usage of capitals in a word to be right when one of the following cases holds:

  • All letters in this word are capitals, like "USA ".
  • All letters in this word are not capitals, like "leetcode ".
  • Only the first letter in this word is capital, like "Google ".

Given a string word, return true if the usage of capitals in it is right.

Example 1:

Input: word = "USA" Output: true

Example 2:

Input: word = "FlaG" Output: false

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
⚠️ Correct
program main
    implicit none

    character(len=*), parameter :: example_1 = "USA"
    character(len=*), parameter :: example_2 = "FlaG"
    character(len=*), parameter :: example_3 = "leetcode"
    character(len=*), parameter :: example_4 = "Google"

    logical :: result

    result = is_capitalized(example_1)
    print '(L1)', result
    result = is_capitalized(example_2)
    print '(L1)', result
    result = is_capitalized(example_3)
    print '(L1)', result
    result = is_capitalized(example_4)
    print '(L1)', result

contains

    function is_capitalized(word) result(is_right)
        implicit none
        character(len=*), intent(in) :: word
        logical :: is_right

        integer :: i

        is_right = .false.

        do i = 1, len(word)
            if (ichar(word(i:i)) >= ichar('A') .and. ichar(word(i:i)) <= ichar('Z')) then
                ! If any letter is capital, then the whole word must be capitalized
                exit
            end if
        end do

        if (i == len(word)) then
            ! If we reach the end of the word without finding any capital letter,
            ! then the word is not capitalized
            is_right = .false.
        else
            ! Otherwise, check if the first letter is capital
            if (ichar(word(1:1)) >= ichar('A') .and. ichar(word(1:1)) <= ichar('Z')) then
                is_right = .true.
            end if
        end if

    end function is_capitalized

end program main
❌ Compiled
❌ Executed
❌ Correct
module main

implicit none

interface

function is_capitalized_right(word) result(is_right)
character(len=*), intent(in) :: word
logical :: is_right
end function is_capitalized_right

end interface

contains

subroutine run_test_cases()

character(len=100) :: word
logical :: is_right

word = "USA"
is_right = is_capitalized_right(word)
if (is_right) then
    write (*,*) "Test case 1: PASSED"
else
    write (*,*) "Test case 1: FAILED"
end if

word = "FlaG"
is_right = is_capitalized_right(word)
if (.not. is_right) then
    write (*,*) "Test case 2: PASSED"
else
    write (*,*) "Test case 2: FAILED"
end if

word = "leetcode"
is_right = is_capitalized_right(word)
if (is_right) then
    write (*,*) "Test case 3: PASSED"
else
    write (*,*) "Test case 3: FAILED"
end if

word = "Google"
is_right = is_capitalized_right(word)
if (is_right) then
    write (*,*) "Test case 4: PASSED"
else
    write (*,*) "Test case 4: FAILED"
end if

end subroutine run_test_cases

function is_capitalized_right(word) result(is_right)
character(len=*), intent(in) :: word
logical :: is_right

! Check if all letters in the word are capitals
if (all(word == ucase(word))) then
    is_right = .true.
! Check if all letters in the word are not capitals
else if (all(word == lcase(word))) then
    is_right = .true.
! Check if only the first letter in the word is capital
else if (word(1:1) == ucase(word(1:1)) .and. &
        & all(word(2:) == lcase(word(2:)))) then
    is_right = .true.
else
    is_right = .false.
end if

end function is_capitalized_right

end module main

! Call the run_test_cases subroutine to run the test cases
call run_test_cases()
🌐 Data from online sources
def detectCapitalUse(word: str) -> bool:
    numCapital = sum(1 for c in word if c.isupper())
    return numCapital == 0 or numCapital == len(word) or (numCapital == 1 and word[0].isupper())
The algorithm keeps track of the count of capital letters in the word. It iterates over each character in the word, and if the character is uppercase, it increments the `numCapital` counter.

After iterating through the word, the algorithm checks if one of the three conditions holds: 1. There are no capital letters (numCapital == 0) 2. All letters are capital (numCapital == word.size() in C++ / word.length in Java and JavaScript / len(word) in Python) 3. Only the first letter is capital (numCapital == 1 and the first letter is uppercase)

If any of these conditions hold, the function returns true, otherwise false.

🌐 Data from online sources
bool detectCapitalUse(string word) {
    int numCapital = 0;
    for (char c : word) {
        if (isupper(c)) numCapital++;
    }
    return numCapital == 0 || numCapital == word.size() || (numCapital == 1 && isupper(word[0]));
}
The algorithm keeps track of the count of capital letters in the word. It iterates over each character in the word, and if the character is uppercase, it increments the `numCapital` counter.

After iterating through the word, the algorithm checks if one of the three conditions holds: 1. There are no capital letters (numCapital == 0) 2. All letters are capital (numCapital == word.size() in C++ / word.length in Java and JavaScript / len(word) in Python) 3. Only the first letter is capital (numCapital == 1 and the first letter is uppercase)

If any of these conditions hold, the function returns true, otherwise false.