我们已经给出了一个字谜字符串,我们必须检查它是否可以变成回文。 例如:
null
Input : geeksforgeeks Output : NoThere is no palindrome anagram of given stringInput : geeksgeeksOutput : YesThere are palindrome anagrams ofgiven string. For example kgeesseegk
这个问题基本上和 检查给定字符串的字符是否可以重新排列以形成回文 .我们可以使用计数数组在O(n)时间内完成。以下是详细的步骤。 1) 创建一个字母表大小的计数数组,通常为256。将计数数组的所有值初始化为0。 2) 遍历给定的字符串并增加每个字符的计数。 3) 遍历计数数组,如果计数数组有多个奇数,则返回false。否则,返回true。
C++
#include <iostream> 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" : cout << "No" ; canFormPalindrome( "geeksogeeks" ) ? cout << "Yes" : cout << "No" ; 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
<script> // Javascript program to Check if any anagram // of a string is palindrome or not let NO_OF_CHARS = 256; /* function to check whether characters of a string can form a palindrome */ function canFormPalindrome(str) { // Create a count array and // initialize all values as 0 let count = new Array(NO_OF_CHARS); count.fill(0); // For each character in input // strings, increment count in // the corresponding count array for (let i = 0; i < str.length; i++) count[str[i].charCodeAt()]++; // Count odd occurring characters let odd = 0; for (let 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 ; } document.write(canFormPalindrome( "geeksforgeeks" )? "Yes" + "</br>" : "No" + "</br>" ); document.write(canFormPalindrome( "geeksogeeks" )? "Yes" : "No" ); // This code is contributed by divyeshrabadiya07. </script> |
输出:
NoYes
本文由 里沙布·贾因 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END