Saturday, December 24, 2022
Detect capital
A function to determine if the usage of capitals in a word is correct.
Go
- Time complexity: - n is a length of a string
- Auxiliary space: - constant amount of space
import "strings"
func detectCapitalUse(word string) bool {
// all lowercase
if (strings.ToLower(word) == word) {
return true
}
// all uppercase
if (strings.ToUpper(word) == word) {
return true
}
// from second letter lowercase
if (strings.ToLower(word[1:]) == word[1:]) {
return true
}
return false
}