알고리즘

[Python,Java] BOJ 10988번 : 팰린드롬인지 확인하기

eunyoung23 2022. 10. 1. 18:18

난이도 : 브론즈 II

 

문제

알파벳 소문자로만 이루어진 단어가 주어진다. 이때, 이 단어가 팰린드롬인지 아닌지 확인하는 프로그램을 작성하시오.

팰린드롬이란 앞으로 읽을 때와 거꾸로 읽을 때 똑같은 단어를 말한다. 

level, noon은 팰린드롬이고, baekjoon, online, judge는 팰린드롬이 아니다.

 

 

입력

첫째 줄에 단어가 주어진다. 단어의 길이는 1보다 크거나 같고, 100보다 작거나 같으며, 알파벳 소문자로만 이루어져 있다.

 

 

출력

첫째 줄에 팰린드롬이면 1, 아니면 0을 출력한다.

 

 

해결방법

문자열을 입력받고 해당 문자열을 거꾸로 정렬한 것과 비교하면 된다고 생각함.

 

 

 

구현 코드

코드1

import sys

input=sys.stdin.readline

N=input().rstrip()

reverse=list(reversed(list(N)))

if reverse==list(N):
    print(1)
else:
    print(0)

 

코드2

import sys

word=sys.stdin.readline().strip()

if word==word[::-1]:
    print("1")
else:
    print("0")

자바 코드1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);

        String word=in.next();

        StringBuffer sb=new StringBuffer(word);
        String reverse=sb.reverse().toString();

        if(word.equals(reverse)){
            System.out.println(1);
        }else{
            System.out.println(0);
        }

    }
}

 

자바 코드2

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String word=in.next();

        StringBuffer sb=new StringBuffer();
        for(int index=word.length()-1; index>=0; index--){
            sb.append(word.charAt(index));
        }

        if(word.equals(sb.toString())){
            System.out.println(1);
        }else{
            System.out.println(0);
        }

    }
}

 

문제 링크

https://www.acmicpc.net/problem/10988

 

10988번: 팰린드롬인지 확인하기

첫째 줄에 단어가 주어진다. 단어의 길이는 1보다 크거나 같고, 100보다 작거나 같으며, 알파벳 소문자로만 이루어져 있다.

www.acmicpc.net