
😎풀이
firstLowerChar
: 첫 번째 문자를 소문자로 변환
secondLowerChar
: 두 번째 문자를 소문자로 변환
- 단어의 첫 부분이 소문자라면
3-1. 모든 문자가 소문자인지 검사
- 단어의 두번째 부분이 소문자라면
4-2. 이후 모든 문자가 소문자인지 검사
- 단어가 모두 대문자인지 검사
function detectCapitalUse(word: string): boolean {
const n = word.length
if(n === 1) return true
const firstLowerChar = word[0].toLowerCase()
const secondLowerChar = word[1].toLowerCase()
if(word[0] === firstLowerChar) {
for(const char of word) {
const curCharCode = char.charCodeAt(0)
if(curCharCode >= 65 && curCharCode < 97) return false
}
return true
}
if(word[1] === secondLowerChar) {
for(let i = 1; i < n; i++) {
const curCharCode = word[i].charCodeAt(0)
if(curCharCode >= 65 && curCharCode < 97) return false
}
return true
}
for(let i = 1; i < n; i++) {
const curCharCode = word[i].charCodeAt(0)
if(curCharCode >= 97) return false
}
return true
};