📌  相关文章
📜  检查在K回文字符串可以从给定的字符串来形成

📅  最后修改于: 2021-10-27 08:57:06             🧑  作者: Mango

给定一个大小为N的字符串S和一个整数K ,任务是找到字符串的字符是否可以同时排列成K 个回文字符串。
例子:

方法

  • 如果每个字符的频率都是偶数,并且 K 介于 1 和 N 之间,那么总是有可能形成 K 个回文字符串。
  • 但是如果有一些字符(比如odd_count )的频率为奇数,那么K 必须位于odd_count 和N 之间才能使K 回文字符串成为可能。

因此,请按照以下步骤解决问题:

如果 K 超过字符串的长度,则直接打印“No”

  1. 将所有字符的频率存储在 Map 中。
  2. 计算具有奇数频率的字符数。
  3. 如果计数小于给定的 K,则打印“否”。否则,打印“是”。

下面是上述方法的实现:

C++
// C++ program to check
// whether the string is
// K palindrome or not
#include 
#include 
using namespace std;
 
// function to check
// whether the string is
// K palindrome or not
bool can_Construct(string S, int K)
{
    // map to frequency of character
    map m;
 
    int i = 0, j = 0, p = 0;
 
    // Check when k is given
    // as same as length of string
    if (S.length() == K) {
        return true;
    }
 
    // iterator for map
    map::iterator h;
 
    // storing the frequency of every
    // character in map
    for (i = 0; i < S.length(); i++) {
        m[S[i]] = m[S[i]] + 1;
    }
 
    // if K is greater than size
    // of string then return false
    if (K > S.length()) {
        return false;
    }
 
    else {
 
        // check that number of character
        // having the odd frequency
        for (h = m.begin(); h != m.end(); h++) {
 
            if (m[h->first] % 2 != 0) {
                p = p + 1;
            }
        }
    }
 
    // if k is less than number of odd
    // frequency character then it is
    // again false other wise true
    if (K < p) {
        return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    string S = "annabelle";
    int K = 4;
 
    if (can_Construct(S, K)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program to check
// whether the string is
// K palindrome or not
import java.util.*;
 
class GFG{
 
// Function to check whether
// the string is K palindrome or not
static boolean can_Construct(String S, int K)
{
     
    // Map to frequency of character
    Map m = new HashMap<>();
     
    int p = 0;
     
    // Check when k is given
    // as same as length of string
    if (S.length() == K)
        return true;
     
    // Storing the frequency of every
    // character in map
    for(int i = 0; i < S.length(); i++)
        m.put(S.charAt(i),
              m.getOrDefault(S.charAt(i), 0) + 1);
               
    // If K is greater than size
    // of then return false
    if (K > S.length())
        return false;
     
    else
    {
         
        // Check that number of character
        // having the odd frequency
        for(Integer h : m.values())
        {
            if (h % 2 != 0)
                p = p + 1;
        }
    }
     
    // If k is less than number of odd
    // frequency character then it is
    // again false otherwise true
    if (K < p)
        return false;
     
    return true;
}
 
// Driver Code
public static void main (String[] args)
{
    String S = "annabelle";
    int K = 4;
     
    if (can_Construct(S, K))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to check whether
# the is K palindrome or not
 
# Function to check whether
# the is K palindrome or not
def can_Construct(S, K):
     
    # map to frequency of character
    m = dict()
    p = 0
     
    # Check when k is given
    # as same as length of string
    if (len(S) == K):
        return True
 
    # Storing the frequency of every
    # character in map
    for i in S:
        m[i] = m.get(i, 0) + 1
 
    # If K is greater than size
    # of then return false
    if (K > len(S)):
        return False
 
    else:
 
        # Check that number of character
        # having the odd frequency
        for h in m:
            if (m[h] % 2 != 0):
                p = p + 1
 
    # If k is less than number of odd
    # frequency character then it is
    # again false otherwise true
    if (K < p):
        return False
 
    return True
 
# Driver code
if __name__ == '__main__':
     
    S = "annabelle"
    K = 4
 
    if (can_Construct(S, K)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program to check
// whether the string is
// K palindrome or not
using System;
using System.Collections.Generic;
class GFG{
 
// Function to check whether
// the string is K palindrome or not
static bool can_Construct(String S,
                          int K)
{    
  // Map to frequency of character
  Dictionary m = new Dictionary();
  int p = 0;
 
  // Check when k is given
  // as same as length of string
  if (S.Length == K)
    return true;
 
  // Storing the frequency of every
  // character in map
  for(int i = 0; i < S.Length; i++)
    if(!m.ContainsKey(S[i]))
      m.Add(S[i], 1);
  else
    m[S[i]] = m[S[i]] + 1;
 
  // If K is greater than size
  // of then return false
  if (K > S.Length)
    return false;
 
  else
  {
    // Check that number of character
    // having the odd frequency
    foreach(int h in m.Values)
    {
      if (h % 2 != 0)
        p = p + 1;
    }
  }
 
  // If k is less than number of odd
  // frequency character then it is
  // again false otherwise true
  if (K < p)
    return false;
 
  return true;
}
 
// Driver Code
public static void Main(String[] args)
{
  String S = "annabelle";
  int K = 4;
  if (can_Construct(S, K))
    Console.WriteLine("Yes");
  else
    Console.WriteLine("No");
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
Yes

时间复杂度: O (N)
辅助空间: O(N)

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