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

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


题目标签:Hash Table

  题目给了我们一个string s,让我们判断它是不是可以变作回文。

  看一下回文特性:

    如果是偶数长度的回文,其中所有的char 都是偶数出现次数;

    如果是奇数长度的回文,那么只可以有一个char 是奇数的出现次数。

  

  只要把char 当作key,它的出现次数当作value 存入HashMap,之后遍历keyset 来验证出现次数就可以了。

Java Solution:

Runtime beats 29.15%

完成日期:11/05/2017

关键词:HashMap

关键点:char 当作 key,出现次数当作 value 存入

 class Solution
{
public boolean canPermutePalindrome(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
int oddChar = 0; if(s.length() % 2 != 0)
oddChar = 1; for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); for(char c: map.keySet())
{
if(oddChar < 0)
return false; if(map.get(c) % 2 != 0)
oddChar--;
} return true;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 266. Palindrome Permutation (回文排列)$的更多相关文章

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

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

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

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

  3. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  5. [LeetCode] Palindrome Permutation 回文全排列

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

  6. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  8. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  9. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. Flask Web 发送邮件单文件

    import os from flask import Flask, render_template, session, redirect, url_for from flask_script imp ...

  2. MySQL 8.*版本 修改root密码

    MySQL .*版本 修改root密码 查看版本:select version() from dual; 1.6. 登录mysql: 登录mysql:(因为之前没设置密码,所以密码为空,不用输入密码, ...

  3. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  4. CAD使用SetxDataDouble写数据(网页版)

    主要用到函数说明: MxDrawEntity::SetxDataDouble 写一个Double扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据 ...

  5. CAD隐藏或显示工具条上的按钮(com接口VB语言)

    主要用到函数说明: MxDrawXCustomFunction::Mx_HideToolBarControl 隐藏或显示工具条上的按钮.详细说明如下: 参数 说明 IN LPCTSTR pszTool ...

  6. PHP数据乱码

    本文主要总结下PHP数据乱码的解决方案 要点:多个不同文件系统里一定要统一编码 [注意] (1)HTML编码与MySQL编码一致: (2)PHP编码与MySQL编码一致: (3)header头发送字符 ...

  7. Linux安装redis并且连接内网的redis

    1.安装redis步骤 1.首先准备工作  [root@10-100-14-130 ~]# yum install gcc-c++   yum install wget 2.推荐进入到linux路径/ ...

  8. mysql5.7报Access denied for xxx@localhost 的解决

    使用root用户登录mysql数据库若如下报错 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  9. Linux系统重要的子目录

    更多目录知识  http://blog.51cto.com/yangrong/1288072 /etc/fstab 机自动挂载分区/磁盘,规定哪个分区/设备,挂载到哪里 [root@oldboy ~] ...

  10. iframe子页面操作父页面并实现屏蔽页面弹出层效果