팰린드롬(palindrome)

문제

설명

앞에서 읽을 때나 뒤에서 읽을 때나 같은 문자열을 팰린드롬이라고 합니다. 문자열이 입력되면 해당 문자열이 팰린드롬이면 “YES”, 아니면 “NO”를 출력하는 프로그램을 작성하세요.

단 회문을 검사할 때 알파벳만 가지고 회문을 검사하며, 대소문자를 구분하지 않습니다. 알파벳 이외의 문자들의 무시합니다.

입력

첫 줄에 길이 100을 넘지 않는 공백이 없는 문자열이 주어집니다.

출력

첫 번째 줄에 팰린드롬인지의 결과를 YES 또는 NO로 출력합니다.

예시 입력 1

found7, time: study; Yduts; emit, 7Dnuof

해결방법

  • StringBuilder 의 reverse() 또는 replaceAll을 통한 정규식으로 문자열 제거 활용 가능.

    // 알파벳(대소문자) 아닌 것을 제거
    String t = s.replaceAll("[^A-Za-z]", "");
    

코드

import java.util.*;

class Main {
    public String solution(String s) {
        String answer = "NO";
        s = s.toUpperCase().replaceAll("[^A-Z]", "");
        String tmp = new StringBuilder(s).reverse().toString();
        if (s.equals(tmp)) answer = "YES";
        return answer;
    }

    public static void main(String[] args) {
        Main.Main T = new Main.Main();
        Scanner kb = new Scanner(System.in);
        String str = kb.nextLine();
        System.out.print(T.solution(str));
    }
}


© 2024. Chiptune93 All rights reserved.