😎풀이

  1. firstLowerChar: 첫 번째 문자를 소문자로 변환
  2. secondLowerChar: 두 번째 문자를 소문자로 변환
  3. 단어의 첫 부분이 소문자라면
    3-1. 모든 문자가 소문자인지 검사
  4. 단어의 두번째 부분이 소문자라면
    4-2. 이후 모든 문자가 소문자인지 검사
  5. 단어가 모두 대문자인지 검사
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
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글