UVA – 401 – Plaindromes

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String[] type = { " -- is not a palindrome.", " -- is a regular palindrome.", " -- is a mirrored string.", " -- is a mirrored palindrome." };
		while (in.hasNext()) {
			String s = in.nextLine().trim();
			System.out.println(s + type[getType(s)] +"\n");
		}
	}

	private static int getType(String s) {
		String a = "AEHIJLMOSTUVWXYZ12358";
		char[] b = "A3HILJMO2TUVWXY51SEZ8".toCharArray();
		
		
		boolean mirrored = true, plaindrome = true;
		for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {
			int indx = a.lastIndexOf(s.charAt(i));
			mirrored &= indx <0 ? false : s.charAt(j) == b[indx];
			plaindrome &= s.charAt(i) == s.charAt(j);
		}
		return (mirrored ? 2 : 0) + (plaindrome ? 1 : 0);
	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: