結果

問題 No.1617 Palindrome Removal
ユーザー ks2m
提出日時 2021-07-22 21:29:04
言語 Java
(openjdk 23)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 670 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 77,972 KB
実行使用メモリ 49,256 KB
最終ジャッジ日時 2024-07-17 16:28:36
合計ジャッジ時間 8,591 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		char[] s = sc.next().toCharArray();
		sc.close();

		int n = s.length;
		boolean all = true;
		boolean kai = true;
		for (int i = 0; i < n; i++) {
			if (s[0] != s[i]) {
				all = false;
			}
			if (s[i] != s[n - 1 - i]) {
				kai = false;
			}
		}
		if (!kai) {
			System.out.println(n);
			return;
		}
		if (all) {
			if (n % 2 == 0) {
				System.out.println(0);
			} else {
				System.out.println(-1);
			}
		} else {
			if (n == 3) {
				System.out.println(-1);
			} else {
				System.out.println(n - 2);
			}
		}
	}
}
0