We define the usage of capitals in a word to be right when one of the following cases holds:
"USA "
."leetcode "
."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.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
T T F T
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()
temp.f95:55:30: 7 | function is_capitalized_right(word) result(is_right) | ...... 55 | function is_capitalized_right(word) result(is_right) | 1 Error: Procedure βis_capitalized_rightβ at (1) is already defined at (2) temp.f95:56:36: 56 | character(len=*), intent(in) :: word | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:57:19: 57 | logical :: is_right | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:60:34: 60 | if (all(word == ucase(word))) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:61:21: 61 | is_right = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:63:39: 63 | else if (all(word == lcase(word))) then | 1 Error: Unexpected ELSE IF statement in CONTAINS section at (1) temp.f95:64:21: 64 | is_right = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:66:16: 66 | else if (word(1:1) == ucase(word(1:1)) .and. & | 1 Error: Syntax error in argument list at (1) temp.f95:68:21: 68 | is_right = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:69:4: 69 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:70:22: 70 | is_right = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:71:3: 71 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:73:3: 73 | end function is_capitalized_right | 1 Error: Expecting END MODULE statement at (1) f951: Error: Unexpected end of file in βtemp.f95β
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
.
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
.