Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

이븐곰의 프로그래밍

String Reverse 본문

java/Algorithm

String Reverse

이븐곰 2020. 6. 2. 10:45

"palindrome"은 반대로 읽어도 원래대로 읽은 것과 동일한 구문, 숫자, 문자열이다.

 

문제 : 입력받은 문자열이 "palindrome"인지 여부를 출력

https://www.hackerrank.com/challenges/java-string-reverse/problem?h_r=next-challenge&h_v=zen

 

Java String Reverse | HackerRank

Learn how to reverse a string. Given a string, determine if its a palindrome.

www.hackerrank.com

 

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        
        sc.close();

        int length = A.length();
        boolean palindrome = true;

        for (int i = 0; i <= length / 2; i++) {
            char a = A.charAt(i);
            char b = A.charAt(length - i - 1);

            if (a != b) {
                palindrome = false;
            }
        }

        if (palindrome == true) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

* 위의 코드에서는 문자열을 배열로 변경해 하나씩 비교하였지만, 다음과 같이 해결하는 것도 가능

 - A를 char list로 변경

 - char list를 Collections.reverse 로 리버스

 - reverse 된 list를 문자열로 변경

 - A와 리버스 문자열 비교

'java > Algorithm' 카테고리의 다른 글

[Leetcode][Easy] 2996. Smallest Missing Integer Greater Than Sequential Prefix Sum  (0) 2024.08.30
[Leetcode][Easy] 14. Longest Common Prefix  (0) 2024.08.30
String Induction  (0) 2020.06.02
SHA-256  (0) 2020.06.01
Regex 2 - Duplicate Words  (0) 2020.06.01