[D2] 초심자의 회문 검사 - 1989
성능 요약
메모리: 20,172 KB, 시간: 137 ms, 코드길이: 894 Bytes
제출 일자
2023-10-16 10:21
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
String word = sc.next();
boolean check = true;
// 0 1 2 3 4 len = 5; len/2 = 2
// 0 == 5 - 0 - 1(4) == true
// 1 == 5 - 1 -1(3) == true
// 0 1 2 len = 3 len / 2 -> 1
// 0 == 3 - 0 - 1(2) == true
//
for(int i = 0; i < word.length() / 2; i++){
char front = word.charAt(i);
char rear = word.charAt(word.length() - i - 1);
if(front != rear){
check = false;
break;
}
}
System.out.printf("#%d %d\n",tc,check==true?1:0);
}
}
}
문자열 인덱싱을 이용한 풀이
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
String word = sc.next();
StringBuilder sb = new StringBuilder(word);
String reverseWord = sb.reverse().toString();
boolean check = true;
if(!word.equals(reverseWord)){
check = false;
}
System.out.printf("#%d %d\n",tc,check==true?1:0);
}
}
}
StringBuilder을 이용한 풀이
'알고리즘 - SWEA > D2' 카테고리의 다른 글
[SW expert Academy] SWEA 1984번 중간 평균값 구하기 자바(Java) (1) | 2023.10.16 |
---|---|
[SW expert Academy] SWEA 1986번 지그재그 숫자 자바(Java) (0) | 2023.10.16 |
[SW expert Academy] SWEA 2001번 파리퇴치 자바(Java) (0) | 2023.10.16 |
[SW expert Academy] SWEA 2007번 패턴 마디의 길이 자바(Java) (0) | 2023.10.16 |
[SW expert Academy] SWEA 2005번 파스칼의 삼각형 자바(Java) (0) | 2023.10.16 |