java solution with int array and even or odd judgement


  • 0
    M

    public boolean canPermutePalindrome(String s) {
    if(s == null || s.length() == 0) return false;
    int[] chs = new int[256];
    int len = s.length();
    for(int i = 0; i < len; i++) {
    chs[s.charAt(i)]++;
    }
    int countone = 0;
    for(int i = 0; i < 256; i++) {
    if(isOdd(chs[i])) countone++;;
    //if(chs[i] == 1) countone++;
    }
    // this step is include 1 or even of all chs array element
    if(countone > 1) return false;
    else if(countone == 1) {
    if(isEven(len)) return false;
    }
    return true;
    }
    public boolean isEven(int num) {
    return num%2 == 0;
    }
    public boolean isOdd(int num) {
    return num%2 == 1;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.