Permutation Palindrome (JAVA)

[Cracking the Coding Interview]

Problem – Given a String, write a function to check if it is a permutation of a palindrome.
ex – Input -> Tact Coa.. Output -> True (taco cat, atco cta etc.)
* LOGIC -> String is palindrome if all letters, except one (at most) are paired.
* If more than one letter unpaired, implies not a palindrome

Input – A string

Output – True or false


import java.util.HashMap;
import java.util.Scanner;

public class PermutationPalindrome {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(;
        System.out.println("Enter string");
        String s = scan.nextLine();
        s= s.toLowerCase();					//ignoring case
        boolean t= checkPermutation(s);

    public static boolean checkPermutation(String s)
        HashMap<Character,Integer> h = new HashMap<>();
        int count=1; char m; int flag=0;
        for(char c : s.toCharArray())

        for(int i = 0; i < s.length(); i++)         {             m = s.charAt(i);             if(m==' ')            // to account for spaces in string                 continue;             count=h.get(m);             if(count==1)             {                 flag++;                 if(flag > 1)
                    return false;

        return true;



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s