📌  相关文章
📜  查找字符串是否为K-Palindrome或不完全使用所有字符一次

📅  最后修改于: 2021-04-22 02:23:49             🧑  作者: Mango

给定一个字符串str和一个整数K,任务是检查是否有可能要使用字符串的所有字符恰好一次用字符串k回文
例子:

方法:

  1. 如果字符串的大小小于k,则不可能获得k回文。
  2. 如果字符串的大小等于k,则总是有可能获得k回文。我们给k个字符串的每个字符串赋予1个字符,它们全部都是回文,因为长度为1的字符串始终是回文。
  3. 如果字符串的长度大于k那么一些字符串可能有超过1个字符。
    • 创建哈希表以存储每个字符的频率,因为偶数出现的字符可以以2为一组分布。
    • 检查出现次数为奇数的字符数是否小于或等于k,可以说总是可以生成k回文。

下面是上述方法的实现:

C++
// C++ program to find if string is K-Palindrome
// or not using all characters exactly once
  
#include 
using namespace std;
  
void iskPalindromesPossible(string s, int k)
{
    // when size of string is less than k
    if (s.size() < k) {
        cout << "Not Possible" << endl;
        return;
    }
  
    // when size of string is equal to k
    if (s.size() == k) {
        cout << "Possible" << endl;
        return;
    }
  
    // when size of string is greater than k
  
    // to store the frequencies of the characters
    map freq;
    for (int i = 0; i < s.size(); i++)
        freq[s[i]]++;
  
    // to store the count of characters
    // whose number of occurrences is odd.
    int count = 0;
  
    // iterating over the map
    for (auto it : freq) {
        if (it.second % 2 == 1)
            count++;
    }
    if (count > k)
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
}
  
// Driver code
int main()
{
  
    string str = "poor";
    int K = 3;
    iskPalindromesPossible(str, K);
  
    str = "geeksforgeeks";
    K = 10;
    iskPalindromesPossible(str, K);
  
    return 0;
}


Java
// Java program to find if String
// is K-Palindrome or not using 
// all characters exactly once
import java.util.*;
  
class GFG{
  
static void iskPalindromesPossible(String s,
                                   int k)
{
      
    // When size of String is less than k
    if (s.length() < k)
    {
        System.out.print("Not Possible" + "\n");
        return;
    }
  
    // When size of String is equal to k
    if (s.length() == k)
    {
        System.out.print("Possible" + "\n");
        return;
    }
      
    // When size of String is greater than k
    // to store the frequencies of the characters
    HashMap freq = new HashMap();
    for(int i = 0; i < s.length(); i++)
       if(freq.containsKey(s.charAt(i)))
       {
           freq.put(s.charAt(i), 
           freq.get(s.charAt(i)) + 1);
       }
       else
       {
           freq.put(s.charAt(i), 1);
       }
  
    // To store the count of characters
    // whose number of occurrences is odd.
    int count = 0;
  
    // Iterating over the map
    for(Map.Entry it : freq.entrySet()) 
    {
       if (it.getValue() % 2 == 1)
           count++;
    }
      
    if (count > k)
        System.out.print("No" + "\n");
    else
        System.out.print("Yes" + "\n");
}
  
// Driver code
public static void main(String[] args)
{
    String str = "poor";
    int K = 3;
    iskPalindromesPossible(str, K);
  
    str = "geeksforgeeks";
    K = 10;
    iskPalindromesPossible(str, K);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Find if string is K-Palindrome or not using all characters exactly once
# Python 3 program to find if string is K-Palindrome
# or not using all characters exactly once
  
def iskPalindromesPossible(s, k):
  
    # when size of string is less than k
    if (len(s) k):
        print("No")
    else:
        print("Yes")
  
# Driver code
if __name__ == '__main__':
    str1 = "poor"
    K = 3
    iskPalindromesPossible(str1, K)
  
    str = "geeksforgeeks"
    K = 10
    iskPalindromesPossible(str, K)
  
# This code is contributed by Surendra_Gangwar


C#
// C# program to find if String
// is K-Palindrome or not using 
// all characters exactly once
using System;
using System.Collections.Generic;
  
class GFG{
  
static void iskPalindromesPossible(String s,
                                   int k)
{
      
    // When size of String is less than k
    if (s.Length < k)
    {
        Console.Write("Not Possible" + "\n");
        return;
    }
  
    // When size of String is equal to k
    if (s.Length == k)
    {
        Console.Write("Possible" + "\n");
        return;
    }
      
    // When size of String is greater than k
    // to store the frequencies of the characters
    Dictionary freq = new Dictionary();
    for(int i = 0; i < s.Length; i++)
        if(freq.ContainsKey(s[i]))
        {
            freq[s[i]] = freq[s[i]] + 1;
        }
        else
        {
            freq.Add(s[i], 1);
        }
  
    // To store the count of characters
    // whose number of occurrences is odd.
    int count = 0;
  
    // Iterating over the map
    foreach(KeyValuePair it in freq) 
    {
        if (it.Value % 2 == 1)
            count++;
    }
      
    if (count > k)
        Console.Write("No" + "\n");
    else
        Console.Write("Yes" + "\n");
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "poor";
    int K = 3;
    iskPalindromesPossible(str, K);
  
    str = "geeksforgeeks";
    K = 10;
    iskPalindromesPossible(str, K);
}
}
  
// This code is contributed by Princi Singh


输出:
Yes
Yes