什么是最好的算法来找到一个字谜是否是回文的?回文、字谜、算法、是最好的

由网友(戒爱)分享简介:在这个问题,我们只考虑字符串的小写英文字母(az)。In this problem we consider only strings of lower-case English letters (a-z).一个字符串是回文如果有字符完全相同的顺序走过时,左到右为从右到左。例如,下面的字符串是回文:A string...

在这个问题,我们只考虑字符串的小写英文字母(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.

阅读全文

相关推荐

最新文章