Given a string, write a function to determine whether it is one of the permutations of a palindrome string. A palindrome is a word or phrase that is the same in both directions. Permutation is the rearrangement of letters. Palindromes are not necessarily dictionary words. Example 1: Input: "tactcoa" Output: true (permutations include "tacocat", "atcocta", etc.) Otherwise false after dinner. Sort first, compare? Use quicksort.Copy the code

1. Canpermutepalindrome. Java:

package com.yuhl.right.leetcode;

import com.yuhl.Main;

import java.util.Arrays;

/ * * *@author yuhl
 * @Date 2020/10/25 8:38
 * @Classname CanPermutePalindrome
 * @DescriptionGiven a string, write a function to determine whether it is one of the permutations of a palindrome string. * Palindromes are words or phrases that are the same in both directions. Permutation is the rearrangement of letters. * Palindromes are not necessarily dictionary words. * * Example 1: * Input: "tactcoa" * output: true (permutations are "tacocat", "atcocta", etc.) */
public class CanPermutePalindrome {
    public static void main(String[] args) {
        String s = "tacocat";
        boolean b = canPermutePalindrome(s);
        System.out.println(b);
    }

    public static boolean canPermutePalindrome(String s) {
        // Only one character can appear once. Other characters must appear twice. Otherwise false after dinner. ,
        // Sort first, compare? Use quicksort.

        char[] arr = s.toCharArray();
        Arrays.sort(arr);


        //arr is an array of sort numbers.
        int dcount = 0;// Records the number of occurrences of a single character, and returns true if 0 or 1. Otherwise return false

        for(int i=0; i<arr.length; i++){
            if(i == arr.length-1) {
                dcount ++;
                break;
            }
            if(arr[i] == arr[i+1]){
                i++;/ / to skip the I + 1;
            } else{ dcount ++; }}return 0 == dcount || 1==dcount; }}Copy the code

2. Execution Results:

"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java exe" 
true
Copy the code