我们给出了一个字谜字符串,我们必须检查它是否可以变成回文。
例子:
Input : geeksforgeeks
Output : No
There is no palindrome anagram of
given string
Input : geeksgeeks
Output : Yes
There are palindrome anagrams of
given string. For example kgeesseegk
这个问题基本上是一样的检查,如果给定的字符串的字符可以重新排列成回文。我们可以使用计数数组在 O(n) 时间内完成。以下是详细步骤。
1) 创建一个字母大小的计数数组,通常为 256。将计数数组的所有值初始化为 0。
2) 遍历给定的字符串并递增每个字符的计数。
3) 遍历count数组,如果count数组有多个奇数,则返回false。否则,返回真。
C++
#include
using namespace std;
#define NO_OF_CHARS 256
/* function to check whether characters of a string
can form a palindrome */
bool canFormPalindrome(string str)
{
// Create a count array and initialize all
// values as 0
int count[NO_OF_CHARS] = { 0 };
// For each character in input strings,
// increment count in the corresponding
// count array
for (int i = 0; str[i]; i++)
count[str[i]]++;
// Count odd occurring characters
int odd = 0;
for (int i = 0; i < NO_OF_CHARS; i++) {
if (count[i] & 1)
odd++;
if (odd > 1)
return false;
}
// Return true if odd count is 0 or 1,
return true;
}
/* Driver program to test to print printDups*/
int main()
{
canFormPalindrome("geeksforgeeks") ? cout << "Yes\n" : cout << "No\n";
canFormPalindrome("geeksogeeks") ? cout << "Yes\n" : cout << "No\n";
return 0;
}
Java
// Java program to Check if any anagram
// of a string is palindrome or not
public class GFG {
static final int NO_OF_CHARS = 256;
/* function to check whether characters of
a string can form a palindrome */
static boolean canFormPalindrome(String str)
{
// Create a count array and initialize
// all values as 0
int[] count = new int[NO_OF_CHARS];
// For each character in input strings,
// increment count in the corresponding
// count array
for (int i = 0; i < str.length(); i++)
count[str.charAt(i)]++;
// Count odd occurring characters
int odd = 0;
for (int i = 0; i < NO_OF_CHARS; i++) {
if ((count[i] & 1) != 0)
odd++;
if (odd > 1)
return false;
}
// Return true if odd count is 0 or 1,
return true;
}
/* Driver program to test to print printDups*/
public static void main(String args[])
{
System.out.println(canFormPalindrome("geeksforgeeks")
? "Yes"
: "No");
System.out.println(canFormPalindrome("geeksogeeks")
? "Yes"
: "No");
}
}
// This code is contributed by Sumit Ghosh
Python
NO_OF_CHARS = 256
""" function to check whether characters of a string
can form a palindrome """
def canFormPalindrome(string):
# Create a count array and initialize all
# values as 0
count = [0 for i in range(NO_OF_CHARS)]
# For each character in input strings,
# increment count in the corresponding
# count array
for i in string:
count[ord(i)] += 1
# Count odd occurring characters
odd = 0
for i in range(NO_OF_CHARS):
if (count[i] & 1):
odd += 1
if (odd > 1):
return False
# Return true if odd count is 0 or 1,
return True
# Driver program to test to print printDups
if(canFormPalindrome("geeksforgeeks")):
print "Yes"
else:
print "No"
if(canFormPalindrome("geeksogeeks")):
print "Yes"
else:
print "NO"
# This code is contributed by Sachin Bisht
C#
// C# program to Check if any anagram
// of a string is palindrome or not
using System;
public class GFG {
static int NO_OF_CHARS = 256;
/* function to check whether
characters of a string can form
a palindrome */
static bool canFormPalindrome(string str)
{
// Create a count array and
// initialize all values as 0
int[] count = new int[NO_OF_CHARS];
// For each character in input
// strings, increment count in
// the corresponding count array
for (int i = 0; i < str.Length; i++)
count[str[i]]++;
// Count odd occurring characters
int odd = 0;
for (int i = 0; i < NO_OF_CHARS; i++) {
if ((count[i] & 1) != 0)
odd++;
if (odd > 1)
return false;
}
// Return true if odd count
// is 0 or 1,
return true;
}
// Driver program
public static void Main()
{
Console.WriteLine(
canFormPalindrome("geeksforgeeks")
? "Yes" : "No");
Console.WriteLine(
canFormPalindrome("geeksogeeks")
? "Yes" : "No");
}
}
// This code is contributed by vt_m.
Javascript
输出:
No
Yes
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。