알고리즘/이것저것
팰린드롬 판단하기
고구마와 감자
2022. 3. 12. 22:14

첫 번째 for문은 공백과 마침표를 걸러내기 위한 for문
그리고 두 번째 for문은 len/2 앞까지만 확인하고 앞뒤로 확인하여 다르다면 false 이상 없으면 true를 반환
boolean solution(String sentence){
String str = "";
for(int i = 0; i < sentence.length(); i++){
char c = sentence.charAt(i);
if(c != ' ' && c != '.') str += c;
}
int len = str.length();
for(int i = 0; i < len / 2; i++){
if(str.charAt(i) != str.charAt(len - 1 - i)) return false;
}
return true;
}