String.define_method :capitalized? do
self == self.downcase ||
self == self.upcase ||
self[0] == self[0].upcase && self[1..] == self[1..].downcase
end
#Challenge
Source | Language | Runtime |
---|---|---|
DailyByte | ruby | \(\mathcal{O}(n)\) |
Check whether a string is capitalised correctly. This is the case if all letters are capitalised, no letters are capitalised or only the first letter is capitalised.
#Test Cases
"USA".capitalized? #=> true
"Calvin".capitalized? #=> true
"compUter".capitalized? #=> false
"coding".capitalized? #=> true