什么是最好的算法来找到一个字谜是否是回文的? [英] What is the best algorithm to find whether an anagram is of a palindrome?

查看:158
本文介绍了什么是最好的算法来找到一个字谜是否是回文的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题,我们只考虑字符串的小写英文字母(az)。

In this problem we consider only strings of lower-case English letters (a-z).

一个字符串是回文如果有字符完全相同的顺序走过时,左到右为从右到左。例如,下面的字符串是回文:

A string is a palindrome if it has exactly the same sequence of characters when traversed left-to-right as right-to-left. For example, the following strings are palindromes:

皮艇

codilitytilidoc

"codilitytilidoc"

neveroddoreven

"neveroddoreven"

一个字符串A是字符串B的字谜如果它由完全一样的人物,但可能在另一种秩序。例如,下面的字符串是彼此的字谜:

A string A is an anagram of a string B if it consists of exactly the same characters, but possibly in another order. For example, the following strings are each other's anagrams:

A =玛丽B =大军A =rocketboysB =octoberskyA =codilityB =codility

A="mary" B="army" A="rocketboys" B="octobersky" A="codility" B="codility"

写一个函数

INT isAnagramOfPalindrome(字符串s);

int isAnagramOfPalindrome(String S);

返回1,如果字符串s是一些回文的字谜,或返回,否则为0。

which returns 1 if the string s is a anagram of some palindrome, or returns 0 otherwise.

例如你的函数应该返回1参数dooernedeevrvn,因为它是一个回文neveroddoreven的字谜。为了论证aabcba,你的函数应该返回0。

For example your function should return 1 for the argument "dooernedeevrvn", because it is an anagram of a palindrome "neveroddoreven". For argument "aabcba", your function should return 0.

推荐答案

算法是太大的话。

您可以从给定的字符集,如果每一个字符出现的,即使设定的次数(可能除了一个字符)构造一个回文。
对于任何其他设置,你可以很容易地证明没有回文存在。

You can construct a palindrome from the given character set if each character occurs in that set even number of times (with possible exception of one character).
For any other set, you can easily show that no palindrome exists.

的证明是在这两种情况下简单,但让我知道,如果这是不明确的。

Proof is simple in both cases, but let me know if that wasn't clear.

这篇关于什么是最好的算法来找到一个字谜是否是回文的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆