Quick actions

cmd+k|ctrl+k

Navigation

Languages

Palindrome Permutation

Snippet info

Language

Java

Visibility

public

Author

arshi.mustafa

Created

2020-10-09T17:08:48Z

Updated

2020-10-11T02:49:04Z

//Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str=s.nextLine();
        Main m = new Main();
        
        boolean palindroms = m.getPermutaionPalindrome(str);
        System.out.println(palindroms);
    }
    
    boolean getPermutaionPalindrome(String a){
        return isPalindrome(a.toCharArray());
    }
    
    boolean isPalindrome(char[] a){
        boolean result = false;
        int[] val = new int[256];//assumption
        char[] letters = new char[256];
        int actual_length = 0;
        for(int i=0;i<a.length ;i++){
            if(val[a[i]] == 0){
                letters[actual_length]=a[i];
                actual_length++;
                
            }
            val[a[i]]++;
            
        }
        boolean one = false;
        for(int j=0; j< actual_length; j++){
            char letter= letters[j];
            int val2 = val[letter];
            System.out.printf("%c count is %d\n",letter,val2);
            if(actual_length%2 == 0 && val2%2 == 0){
                continue;
            }else if(actual_length%2 == 0 && val2%2 != 0){
                return false;
            }else if(actual_length%2 != 0 && val2%2 == 0){
                continue;
            }else if(actual_length%2 != 0 && val2%2 != 0){
                if(one == true){
                    return false;
                }else{
                    one = true;
                }
            }
            
        }
        
        return true;
    }
    
    
}
INFO