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

CODE –


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.in);
        System.out.println("Enter string");
        String s = scan.nextLine();
        s= s.toLowerCase();					//ignoring case
        boolean t= checkPermutation(s);
        System.out.println(t);
    }

    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())
        {
            if(h.containsKey(c)==true)
            {
                count=h.get(c);
                h.put(c,count+1);
            }

            else
                h.put(c,1);
        }
        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;

    }

}
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