섹션 1. String 문자열 : 유효한 팰린드롬
회문 문자열이면 "YES", 아니면 "NO"를 출력
알파벳만 가지고 회문 검사하며, 대소문자 구분하지 않는다.
💻 강의 코드
class Algorithm {
public String solution(String str){
String answer = "NO";
str = str.toUpperCase().replaceAll("[^A-Z]", "");
String tmp = new StringBuilder(str).reverse().toString();
if(str..equals(tmp)) {
answer = "YES";
}
return answer;
}
public static void main(String[] args){
Algorithm T = new Algorithm();
Scanner kb = new Scanner(System.in);
String str = kb.nextLine();
System.out.println(T.solution(str));
}
}
💻 IntelliJ - Service
@Service
public class StringAlgorithmService{
public String isPalindrome2(String str) {
String answer = "NO";
str = str.toUpperCase().replaceAll("[^A-Z]", "");
String s = new StringBuilder(str).reverse().toString();
if (str.equals(s)) {
System.out.println(s);
answer = "YES";
}
return answer;
}
}
@Test
@DisplayName("회문 문자열2")
void palindromeTest2() {
String str = "found7, time: study; Yduts; emit, 7Dnuof";
String answer = stringAlgorithmService.isPalindrome2(str);
System.out.println("result ====> " + answer);
}
💻 결과

💻 NOTE
replace("A", "B") : A는 변환 하고자 하는 대상, B는 변활할 문자 값
replaceAll(String regex, String replacement) : 첫 번째 매개변수-변환하고자 하는 대상, 두 번째 매개변수-변환할 문자값
출처 : 인프런 자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비