Is string permutation a palindrome or not ?
Given a string, write a function to check if it is a permutation of palindrome.
Input : abcabcz
Output : trueInput : abcabcxz
Output : false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.omt.learn.algo; import java.util.Arrays; public class PalindromePermutation { public static void main(String[] args) { System.out.println(isPermutationPalindrome("abcabcz")); } public static boolean isPermutationPalindrome(String s) { char c[] = s.toCharArray(); Arrays.sort(c); int oddCount = 0; for (int i = 0; i < c.length; i++) { if (i + 1 >= c.length) { oddCount++; continue; } if (c[i] == c[i + 1]) { i++; } else { oddCount++; } } return oddCount < 2; } } |