Problem:
Score : 800 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Determine whether we can turn S into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.

Constraints

1 \leq |S| \leq 2 × 10^5

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If we cannot turn S into a palindrome, print -1. Otherwise, print the minimum required number of operations.

Sample Input 1

eel

Sample Output 1

1

We can turn S into a palindrome by the following operation:

Swap the 2-nd and 3-rd characters. S is now ele.

Sample Input 2

ataatmma

Sample Output 2

4

We can turn S into a palindrome by the following operation:

Swap the 5-th and 6-th characters. S is now ataamtma.

Swap the 4-th and 5-th characters. S is now atamatma.

Swap the 3-rd and 4-th characters. S is now atmaatma.

Swap the 2-nd and 3-rd characters. S is now amtaatma.

Sample Input 3

snuke

Sample Output 3

-1

We cannot turn S into a palindrome.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = scan.nextLine();
		
		ArrayList<Character> strs = new ArrayList<Character>();
		ArrayList<Integer> str_nums = new ArrayList<Integer>();
		
		int index;
		for(int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if ((index = strs.indexOf(c)) == -1) {
				strs.add(c);
				str_nums.add(1);
			}else {
				str_nums.set(index, str_nums.get(index) + 1);
			}
		}
		
		int count = 0;
		char last = 's';
		ArrayList<Character> kaibun = new ArrayList<Character>();
		for (int i = 0; i < str_nums.size(); i++) {
			if (str_nums.get(i) % 2 == 1) {
				count++;
				last = strs.get(i);
			}else {
				for (int j = 0; j < str_nums.get(i)/2; j++) kaibun.add(strs.get(i));
			}
			if (count >= 2) {
				System.out.println("-1");break;
			}
		}
		
		if(count <= 1) {
			count = 0;
			char[] strA = str.toCharArray();
			int i = 0, j = strA.length-1, k;
			for (i = 0; i < strA.length / 2; i++) {
				if (strA[i] != strA[j]) {
					if(strA[j] != last) {
						k = i+1;
						while(strA[j] != strA[k])k++;
						strA = charMove(strA, k, i);
						count += k - i;
					}else {
						k = j-1;
						while(strA[i] != strA[k])k--;
						strA = charMove(strA, j, k);
						count += j - k;
					}
				}
				j--;
			}
			//System.out.println(strA);
			System.out.println(count);
		}
	}
	
	public static char[] charMove(char[] str, int start, int end) {
		char t;
		if(end - start > 0) {
			for(int i = start; i < end-1; i++) {
				t = str[i];
				str[i] = str[i+1];
				str[i] = t;
			}
		}else {
			for(int i = start; i >= end+1; i--) {
				t = str[i];
				str[i] = str[i-1];
				str[i-1] = t;
			}
		}
		return str;
	}
}