설명
앞에서 읽을 때나 뒤에서 읽을 때나 같은 문자열을 팰린드롬
문자열이 입력되면 해당 문자열이 팰린드롬이면 "YES", 아니면 “NO"를 출력하는 프로그램을 작성
단 회문을 검사할 때 알파벳만 가지고 회문을 검사하며, 대소문자를 구분 X
알파벳 이외의 문자들의 무시
예시 입력 1
found7, time: study; Yduts; emit, 7Dnuof
예시 출력 1
YES
import java.util.*;
public class 유효한팰린드롬 {
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) {
유효한팰린드롬 T = new 유효한팰린드롬();
Scanner sb = new Scanner(System.in);
String str = sb.nextLine();
System.out.println(T.solution(str));
}
}
'알고리즘 > 코테' 카테고리의 다른 글
순열연습, 조합연습 (알고리즘 JAVA) (0) | 2022.05.12 |
---|---|
[회문문자] (0) | 2021.10.11 |
[중복문자열제거] (0) | 2021.10.11 |
[특정문자뒤집기] (0) | 2021.10.11 |
[단어뒤집기] stringBuilder, reverse 사용 (0) | 2021.10.11 |