/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function (word) {
if (word.length === 1) {
return true;
}
const isAllLowerCase = (word) => {
return word.charCodeAt(0) >= 97 && word.charCodeAt(0) <= 122;
};
const isAllUpperCase = (word) => {
return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
};
if (isAllLowerCase(word[0])) {
for (let i = 0; i < word.length; i++) {
if (!isAllLowerCase(word[i])) {
return false;
}
}
} else {
if (isAllLowerCase(word[1])) {
for (let i = 1; i < word.length; i++) {
if (!isAllLowerCase(word[i])) {
return false;
}
}
} else {
for (let i = 0; i < word.length; i++) {
if (!isAllUpperCase(word[i])) {
return false;
}
}
}
}
return true;
};
Runtime: 97 ms, faster than 63.01% of JavaScript online submissions for Detect Capital.
Memory Usage: 42.4 MB, less than 38.36% of JavaScript online submissions for Detect Capital.