原题

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

For example,

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

解析

判断是否可以组成回文

给一个字符串,判断字符串中的字符是否可以组成回文

思路

其实思路都一样,计算字符串中的字符个数;若字符串的字符有偶数个,则每个字符的出现次数需要有偶数次;若字符串的字符有奇数个,则最多只能有一个字符出现过奇数次,其他字符都需要出现偶数次

解法1:利用Map 的key的唯一性

public boolean canPermutePalindromeOther1(String s) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}
int singleCount = 0;
for (Character c : map.keySet()) {
if ((map.get(c) & 1) != 0) {
singleCount++;
}
if (singleCount > 1) {
break;
}
}
return singleCount <= 1;
}

解法2:利用Set元素的唯一性

public boolean canPermutePalindromeOther2(String s) {
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (!set.add(s.charAt(i))) {
set.remove(s.charAt(i));
}
}
return set.size() <= 1;
}

【leetcode】266. Palindrome Permutation的更多相关文章

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

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

  2. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  3. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  4. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  5. 【LeetCode】234. Palindrome Linked List 解题报告(Python)

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

  6. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  7. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

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

  8. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. 【leetcode】Shortest Palindrome(hard)★

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

随机推荐

  1. RabbitMQ 入门教程(PHP版) 第二部分:工作队列(Work queues)

    工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...

  2. 毫无PS痕迹 你的第一本Photoshop书 完整版

    毫无PS痕迹 你的第一本Photoshop书 目录 <毫无PS痕迹-你的本Photoshop书>全书分为四大部分: 第1.2章讲解色彩和图像的原理与基础知识要点. 第3至11章全面讲解了使 ...

  3. java 连接 mongodb 及使用

    MongoDB是当今非常流行的一款NoSQL数据库,本文介绍如何使用MongoDB的Java驱动来操作MongoDB. 一.引入MongoDB Java Driver包 如果需要操作MongoDB的J ...

  4. bert论文笔记

    摘要 BERT是“Bidirectional Encoder Representations from Transformers"的简称,代表来自Transformer的双向编码表示.不同于 ...

  5. 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】

    本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...

  6. linux的fcntl函数

    fcntl可实现对指定文件描述符的各种操作,例如获取/设置 文件的是否可以被读写的状态,等其他状态. int fcntl (int __fd, int __cmd, ...);The remainin ...

  7. (十六)springMvc 补充

    文章目录 数据回显 `@ModelAttribute` && `@SessionAttributes` 注解 数据回显 对 pojo 数据回显的支持 ,springMvc 会默认的将传 ...

  8. 使用pyinstaller编译python文件

    1.安装pyinstaller pip install pyinstaller 2.编译 pyinstaller yourprogram.py 具体操作   1.编译 d: cd python pyi ...

  9. 关于python的一次性能调优过程

    问题 这两天在公司帮老大写一个程序功能,要求抓取从elasticsearch和kibana服务器上返回的数据,统计所有hits的数据字段ret_code为0的hit,并计算其占有率等一些功能. 功能倒 ...

  10. 05 多继承、object类

    多继承 Python中一个类可以继承多个父类,并且获得全部父类的属性和方法. class A: def demo(self): print("demo") class B: def ...