266. Palindrome Permutation

2024-01-04 19:18
文章标签 permutation 266 palindrome

本文主要是介绍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.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

Solution

Brute-force会超时

class Solution {
public:bool isPalindrome(const string& s){int i = 0;int j = s.length() - 1;while(i<=j){if(s[i] != s[j])return false;++i;--j;}return true;}bool canPermutePalindrome(string s) {sort(s.begin(),s.end());do{if(isPalindrome(s))return true;}while(next_permutation(s.begin(),s.end()));return false;}
};

根据hint,使用map

看了讨论区

class Solution {
public:bool canPermutePalindrome(string s) {map<char,int> charCount;for(int i = 0;i<s.length();++i){charCount[s[i]]++;}int count = 0;for(map<char,int>::iterator it = charCount.begin();it != charCount.end();++it){if(it->second % 2)++count;if(count > 1)return false;}return true;}
};

这篇关于266. Palindrome Permutation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/570329

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

leetCode#125. Valid Palindrome

Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 234. Palindrome Linked List

题:https://leetcode.com/problems/palindrome-linked-list/description/ 题目 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2Output: false Example 2: Input: 1->2->

【ZOJ】3874 Permutation Graph 【FFT+CDQ分治】

传送门:【ZOJ】3874 Permutation Graph 题目分析: 容易知道一个个连通块内部的标号都是连续的,否则一定会有另一个连通块向这个连通块建边,或者这个连通块向另一个连通块建边。而且从左到右左边的连通块内最大的标号小于右边连通块内最小的标号。 然后我们可以构造dp方程: dp[n]=n!−i!∗dp[n−i] \qquad \qquad dp[n] = n! - i! *

LeetCode 31 Next Permutation

题意: 给出一串数字,求该排列的下一个排列。如果该排列为字典序最大排列,则输出字典序最小排列。 思路: 首先C++里面有求下一个排列的函数next_permutation,该函数返回0即表示当前排列字典序最大。 如果要自己动手实现,那么就考虑“如何找到比当前排列大1的排列”。 假设排列A(aaaXddd)比排列B(aaaYfff)大1,a部分为两排列相同部分,X与Y表示最靠左边不同

Count Palindrome in String

string 有多少palindrome substring。exp: 'aba' 返回4 , 'abba' 返回6 public class CountPalindrome {public int countPalindrome(String str){if(str == null || str.length() == 0) return 0;int count = 0;for(int

String Permutation

Given two strings, write a method to decide if one is a permutation of the other. Example Example 1:Input: "abcd", "bcad"Output: TrueExample 2:Input: "aac", "abc"Output: False 思路:count比较一下就可以;

leetcode 刷题之路 28 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 判断一个数字是否是回文数字。 测试通过程序: class Solution {public:bool isPalindrome(int x){int revX=0;int xTemp=x;while (xTemp > 0){int tem