[LeetCode#266] Palindrome Permutation

Problem:

Given a string, determine if a permutation of the string could form a palindrome.

For example,
"code" -> False, "aab" -> True, "carerac" -> True.

General Analysis:

This problem is easy.
Basic idea is:
iff s with odd characters, only one character is allowed to appear odd times.
iff s with even characters, each character should appear even times. 

Wrong Solution 1:

public class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null)
            throw new IllegalArgumentException("s is null");
        int len = s.length();
        if (len <= 1)
            return true;
        boolean[] odd_flag = new boolean[26];
        int odd_count = 0;
        for (int i = 0; i < len; i++) {
            int index = s.charAt(i) - ‘a‘;
            if (odd_flag[index]) {
                odd_flag[index] = false;
                odd_count--;
            } else{
                odd_flag[index] = true;
                odd_count++;
            }
        }
        if (odd_count >= 2)
            return false;
        else
            return true;
    }
}

Mistake Analysis 1:

Runtime Error Message:
Line 12: java.lang.ArrayIndexOutOfBoundsException: -32
Last executed input:
"AaBb//a"

Mistake analysis:
Lack throughly understanding of the problem, the problem does not say the character only appears in the range from ‘a‘ to ‘z‘.

Wrong Solution 2:

public class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null)
            throw new IllegalArgumentException("s is null");
        int len = s.length();
        if (len <= 1)
            return true;
        boolean[] odd_flag = new boolean[128];
        int odd_count = 0;
        for (int i = 0; i < len; i++) {
            int index = s.charAt(i) - ‘0‘;
            if (odd_flag[index]) {
                odd_flag[index] = false;
                odd_count--;
            } else{
                odd_flag[index] = true;
                odd_count++;
            }
        }
        if (odd_count >= 2)
            return false;
        else
            return true;
    }
}

Mistake Analysis 2:

Runtime Error Message:
Line 46: java.lang.ArrayIndexOutOfBoundsException: -1
Last executed input:
"AaBb//a"

Mistakes:
https://simple.wikipedia.org/wiki/ASCII
Even though the length of the ASCII table is 128, the firsrt character in the table is not ‘0‘, but null. You should not do it in such ulgy way!

Analysis 2:

Since each chracter is in the range of [0, 255], why not directly use it for indexing element???
boolean[] odd_flag = new boolean[256];
int odd_count = 0;
for (int i = 0; i < len; i++) {
    char c = s.charAt(i);
    if (odd_flag[c]) {
    ..
    }
}

Solution:

public class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null)
            throw new IllegalArgumentException("s is null");
        int len = s.length();
        if (len <= 1)
            return true;
        boolean[] odd_flag = new boolean[256];
        int odd_count = 0;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (odd_flag[c]) {
                odd_flag[c] = false;
                odd_count--;
            } else{
                odd_flag[c] = true;
                odd_count++;
            }
        }
        if (odd_count >= 2)
            return false;
        else
            return true;
    }
}
时间: 2024-08-16 03:16:53

[LeetCode#266] Palindrome Permutation的相关文章

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

266. Palindrome Permutation

/* *266. Palindrome Permutation *2016-6-24 by Mingyang *这个题目很简单的用Hashmap来计算,而高手用的是HashSet来做,遇到一样的,就remove,从没出现的就Add *不过自己的代码还借鉴了如何loop hashmap的value */ public boolean canPermutePalindrome(String s) { int len=s.length(); if(s==null||len==0) return tru

[LeetCode#267] Palindrome Permutation II

Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s

266. Palindrome Permutation - Easy

Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: "code" Output: false Example 2: Input: "aab" Output: true Example 3: Input: "carerac" Output: true 扫一遍s,统计频率,奇数频率最多只能出现一次.用cn

[LeetCode] Palindrome Permutation I &amp; II

Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even le

LeetCode Longest Palindrome

原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa&qu

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ