📌  相关文章
📜  检查字符串可以重新排列,以便每个奇数长度的子串都是回文

📅  最后修改于: 2021-10-27 03:18:17             🧑  作者: Mango

给定一个字符串S 。任务是检查是否可以重新排列字符串,使得每个奇数长度的子串都是回文。
例子:

方法:

  • 第一个观察是如果字符串的所有字符都相同,那么每个奇数长度的子串都是回文,我们不需要重新排列它们。
  • 第二个观察结果是,如果不同字符的数量超过2 ,则无法重新排列。
  • 现在,如果不同字符的数量正好是2,那么为了让所有奇数长度的子串成为回文,它们的计数差必须小于或等于1 ,如果这满足,那么我们以另一种方式重新排列字符串意味着

s_{i-1}=s_{i+1}

  • 对于i <— ( 1 到 n – 1 ) 。其中n是字符串的长度。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
#include 
using namespace std;
 
// Function to check is it
// possible to rearrange the string
// such that every odd length
// substring is palindrome
bool IsPossible(string s)
{
 
    // Length of the string
    int n = s.length();
 
    // To count number of distinct
    // character in string
    set count;
 
    // To count frequency of
    // each character
    map map;
 
    for (int i = 0; i < n; i++) {
 
        // Inserting into set
        count.insert(s[i]);
 
        // Incrementing the frequency
        map[s[i]] += 1;
    }
 
    // All characters in
    // the string are same
    if (count.size() == 1) {
        return true;
    }
 
    // There are more than 2 different
    // character in string
    if (count.size() > 2) {
        return false;
    }
 
    // Currently there is 2 different
    // character in string
    auto it = count.begin();
 
    // Get the frequencies of the
    // characters that present
    // in string
    int x = 0, y = 0;
    x = map[*it];
 
    it++;
    y = map[*it];
 
    // Difference between their
    // count is less than or
    // equal to 1
    if (abs(x - y) <= 1) {
        return true;
    }
 
    return false;
}
 
// Driver code
int main()
{
    string s = "aaaddad";
 
    if (IsPossible(s))
        cout << "YES\n";
    else
        cout << "NO\n";
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to check is it
    // possible to rearrange the string
    // such that every odd length
    // substring is palindrome
    static boolean IsPossible(String s)
    {
 
        // Length of the string
        int n = s.length();
 
        // To count number of distinct
        // character in string
        HashSet count = new HashSet<>();
 
        // To count frequency of
        // each character
        HashMap map = new HashMap<>();
 
        for (int i = 0; i < n; i++)
        {
 
            // Inserting into set
            count.add(s.charAt(i));
 
            // Incrementing the frequency
            map.put(s.charAt(i), map.get(s.charAt(i)) ==
                    null ? 1 : map.get(s.charAt(i)) + 1);
        }
 
        // All characters in
        // the string are same
        if (count.size() == 1)
            return true;
 
        // There are more than 2 different
        // character in string
        if (count.size() > 2)
            return false;
 
        String newString = count.toArray().toString();
 
        // Currently there is 2 different
        // character in string
        int j = 0;
        char it = newString.charAt(j);
 
        // Get the frequencies of the
        // characters that present
        // in string
        int x = 0, y = 0;
        x = map.get(it) == null ? 0 : map.get(it);
        j++;
 
        it = newString.charAt(j);
        y = map.get(it) == null ? 0 : map.get(it);
 
        // Difference between their
        // count is less than or
        // equal to 1
        if (Math.abs(x - y) <= 1)
            return true;
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "aaaddad";
        if (IsPossible(s))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of
# the above approach
 
# Function to check is it
# possible to rearrange the string
# such that every odd length
# substring is palindrome
def IsPossible(s) :
 
    # Length of the string
    n = len(s);
 
    # To count number of distinct
    # character in string
    count = set();
     
    # To count frequency of
    # each character
    map = dict.fromkeys(s, 0);
 
    for i in range(n) :
 
        # Inserting into set
        count.add(s[i]);
 
        # Incrementing the frequency
        map[s[i]] += 1;
 
    # All characters in
    # the string are same
    if (len(count) == 1) :
        return True;
 
    # There are more than 2 different
    # character in string
    if (len(count) > 2) :
        return False;
         
    # Currently there is 2 different
    # character in string
    j = 0
    it = list(count)[j];
 
    # Get the frequencies of the
    # characters that present
    # in string
    x = 0; y = 0;
    x = map[it];
     
    j += 1
    it = list(count)[j];
    y = map[it];
 
    # Difference between their
    # count is less than or
    # equal to 1
    if (abs(x - y) <= 1) :
        return True;
 
    return False;
 
# Driver code
if __name__ == "__main__" :
 
    s = "aaaddad";
 
    if (IsPossible(s)) :
        print("YES");
    else :
        print("NO");
         
# This code is contributed by AnkitRai01


C#
// C# implementation of the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to check is it
// possible to rearrange the string
// such that every odd length
// substring is palindrome
static bool IsPossible(String s)
{
 
  // Length of the string
  int n = s.Length;
 
  // To count number of distinct
  // character in string
  HashSet count =
          new HashSet();
 
  // To count frequency of
  // each character
  Dictionary map = new Dictionary();
 
  for (int i = 0; i < n; i++)
  {
    // Inserting into set
    count.Add(s[i]);
 
    // Incrementing the frequency
    if(map.ContainsKey(s[i]))
      map[s[i]] = map[s[i]] + 1;
    else
      map.Add(s[i], 1);
  }
 
  // All characters in
  // the string are same
  if (count.Count == 1)
    return true;
 
  // There are more than 2 different
  // character in string
  if (count.Count > 2)
    return false;
 
  String newString = count.ToString();
 
  // Currently there is 2 different
  // character in string
  int j = 0;
  char it = newString[j];
 
  // Get the frequencies of the
  // characters that present
  // in string
  int x = 0, y = 0;
  x = !map.ContainsKey(it) ?
       0 : map[it];
  j++;
 
  it = newString[j];
  y = !map.ContainsKey(it) ?
       0 : map[it];
 
  // Difference between their
  // count is less than or
  // equal to 1
  if (Math.Abs(x - y) <= 1)
    return true;
  return false;
}
 
// Driver Code
public static void Main(String[] args)
{
  String s = "aaaddad";
  if (IsPossible(s))
    Console.WriteLine("YES");
  else
    Console.WriteLine("NO");
}
}
 
// This code is contributed by 29AjayKumar


输出:
YES



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