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;
}
}

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

  1. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

  2. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  3. LeetCode 266. Palindrome Permutation (回文排列)$

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

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

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode#267] Palindrome Permutation II

    Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  6. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...

  7. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 266. Palindrome Permutation

    题目: Given a string, determine if a permutation of the string could form a palindrome. For example,&q ...

  9. 266. Palindrome Permutation 重新排列后是否对称

    [抄题]: Given a string, determine if a permutation of the string could form a palindrome. For example, ...

随机推荐

  1. CentOS7上GitLab的使用

    生成SSH Keys 生成root账号的ssh key # ssh-keygen -t rsa -C "admin@example.com" 显示pub key的值 # cat ~ ...

  2. javascript常用插件

    Math.uuid.js 功能:js 版生成uuid 地址:http://www.broofa.com/ jquery.waterfall.js 功能:瀑布布局图片 地址:https://github ...

  3. codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)

    题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...

  4. gitcafe 使用hexo搭建博客

    --缘由:因为看大家都用github等搭建博客,作为半个程序员的自己,也按捺不住了,终于有空来尝试一把了,选择了和github 相同功能的gitcafe网站,因为在国内比较快,这是大家的看法,下面写一 ...

  5. HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏

    1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...

  6. Android 设计随便说说之简单实践(模块划分)

    上篇随笔随(Android 设计随便说说)便说了一下什么是设计以及设计的原则,这里举一个简单的例子来进一步的说Android设计.我们以应用商店的设计来举例. 在设计之前,需要把握两部分内容,才能使得 ...

  7. python基础知识十

    特殊的方法 在类中有一些特殊的方法具有特殊的意义,比如__init__和__del__方法,它们的重要性我们已经学习过了. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[k ...

  8. python基础知识六

    博客园的博文对每篇博文的长度似乎做了限制 面向对象编程, 在程序何种,根据操作数据的函数或语句块来设计程序.这被成为面向过程的编程.还有一种把数据和功能结合起来,用称为对象的东西包裹起来组织组织程序的 ...

  9. maven 创建的符号连接命令

    E:\Joyplus\src\main\webapp\WEB-INF>mklink /d lib ..\..\..\.\..\target\ediHelperSuite-0.5\WEB-INF\ ...

  10. 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签

    学习jsp不得不学习jsp标签,一般来说,对于一个jsp开发者,可以理解为jsp页面中出现的java代码越少,对jsp的掌握就越好,而替换掉java代码的重要方式就是使用jsp标签.  jsp标签的分 ...