현재

8. 유효한 팰린드롬 본문

알고리즘/기타알고리즘문제

8. 유효한 팰린드롬

AAAge 2024. 4. 3. 20:50

설명

알파벳이외의 문자 무시, 앞으로 읽으나 뒤로 읽었을때 같으면 YES 아니면 NO

예시 입력 1 

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

예시 출력 1

YES

 

<힌트 API>

더보기

replaceAll( "정규식" , "대체할 문자열" ) [^A-Z] 에서 ^는 아니라는 뜻이다.

 

<정답풀이>

더보기
import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        String input  = sc.nextLine();
        String answer = "NO";
        input = input.toUpperCase().replaceAll("[^A-Z]","").toString(); // 정규식 ^ 는 A-Z가 아니면 이라는 뜻이다
        String temp = new StringBuilder(input).reverse().toString();
        if (input.equals(temp)) answer = "YES";

        System.out.println(answer);

        sc.close();
    }
}

'알고리즘 > 기타알고리즘문제' 카테고리의 다른 글

11. 문자열 압축  (0) 2024.04.08
10. 가장 짧은 문자거리  (0) 2024.04.08
7. 회문 문자열  (0) 2024.04.03
6. 중복문자제거(indexOf())  (0) 2024.04.03
5. 특정 문자 뒤집기(Character.isAlphabetic())  (0) 2024.04.03