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

📅  最后修改于: 2021-10-26 05:39:46             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程