문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
단어에 대문자를 사용하는 것이 올바른 경우는 다음과 같은 경우 중 하나에 해당할 때로 정의한다.
문자열 word가 주어졌을 때, 대문자 사용이 올바르면 true를 반환해라.
#1
Input: word = "USA"
Output: true
#2
Input: word = "Flag"
Output: false
class Solution {
public boolean detectCapitalUse(String word) {
if(word.length() == 0 || word.length() == 1) return true;
boolean isFirstUpper = Character.isUpperCase(word.charAt(0));
boolean isSecondUpper = Character.isUpperCase(word.charAt(1));
if(isFirstUpper == false && isSecondUpper == true){
return false;
}
for(int i = 2; i < word.length(); i++){
if(isFirstUpper == true && isSecondUpper == true && Character.isUpperCase(word.charAt(i)) == false){
return false;
}else if(isFirstUpper == true && isSecondUpper == false && Character.isUpperCase(word.charAt(i)) == true){
return false;
}else if(isFirstUpper == false && isSecondUpper == false && Character.isUpperCase(word.charAt(i)) == true){
return false;
}
}
return true;
}
}