给定一个由小写字母组成的字符串S ,任务是检查给定的字符串是否可以重新排列,以便可以将字符串拆分为至少长度为 2 的非重叠回文子字符串。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: S = “aaaabbcdd”
Output: Yes
Explanation: Rearrange the given string S to “acaaabbdd”, which can be split into non-overlapping palindromic substrings “aca”, “aa”, “bb”, “dd”.
Input: S = “ccddgggggefs”
Output: No
方法:可以通过将字符串的字符重新排列为长度为 2 的子串来解决给定的问题,这些子串由单个不同的字符组成。如果存在任何频率为奇数的字符,则将它们放在长度为 2的回文子串的中间。
请按照以下步骤解决问题:
- 初始化一个辅助数组,比如大小为26 的frequency[] ,以存储字符串S 中存在的每个字符的频率。
- 遍历给定的字符串S并更新数组frequency[]中每个字符的频率。
- 初始化两个变量,比如奇数和偶数都为0 ,以存储奇数元素的频率和形成的长度为 2的回文子串的数量。
- 遍历数组frequency[]并且如果frequency[i]的值大于0 ,则将值(frequency[i] & 1)和(frequency[i] / 2) 分别添加到变量odd和even 中。
- 完成上述步骤后,如果odd的值最多为偶数,则打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if a string can be
// modified such that it can be split into
// palindromic substrings of length >= 2
void canSplit(string& S)
{
// Stores frequencies of characters
vector frequency(26, 0);
int cnt_singles = 0;
int k = 0;
// Traverse the string
for (int i = 0; i < S.length(); i++)
// Update frequency
// of each character
frequency[S[i] - 'a']++;
int odd = 0, eve = 0;
// Traverse the frequency array
for (int i = 0; i < 26; i++) {
// Update values of odd and eve
if (frequency[i]) {
odd += (frequency[i] & 1);
eve += frequency[i] / 2;
}
}
// Print the result
if (eve >= odd)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
string S = "aaabbbccc";
canSplit(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check if a string can be
// modified such that it can be split into
// palindromic substrings of length >= 2
static void canSplit(String S)
{
// Stores frequencies of characters
int frequency[] = new int[26];
int cnt_singles = 0;
int k = 0;
// Traverse the string
for (int i = 0; i < S.length(); i++)
// Update frequency
// of each character
frequency[S.charAt(i) - 'a']++;
int odd = 0, eve = 0;
// Traverse the frequency array
for (int i = 0; i < 26; i++) {
// Update values of odd and eve
if (frequency[i] != 0) {
odd += (frequency[i] & 1);
eve += frequency[i] / 2;
}
}
// Print the result
if (eve >= odd)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String[] args)
{
String S = "aaabbbccc";
canSplit(S);
}
}
Python3
# Python3 program for the above approach
# Function to check if a string can be
# modified such that it can be split into
# palindromic substrings of length >= 2
def canSplit(S):
# Stores frequencies of characters
frequency = [0] * 26
cnt_singles = 0
k = 0
# Traverse the string
for i in range(len(S)):
# Update frequency
# of each character
frequency[ord(S[i]) - ord('a')] += 1
odd = 0
eve = 0
# Traverse the frequency array
for i in range(26):
# Update values of odd and eve
if (frequency[i]):
odd += (frequency[i] & 1)
eve += frequency[i] // 2
# Print the result
if (eve >= odd):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == "__main__" :
S = "aaabbbccc"
canSplit(S)
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if a string can be
// modified such that it can be split into
// palindromic substrings of length >= 2
static void canSplit(string S)
{
// Stores frequencies of characters
int []frequency = new int[26];
int cnt_singles = 0;
int k = 0;
// Traverse the string
for (int i = 0; i < S.Length; i++)
// Update frequency
// of each character
frequency[S[i] - 'a']++;
int odd = 0, eve = 0;
// Traverse the frequency array
for (int i = 0; i < 26; i++)
{
// Update values of odd and eve
if (frequency[i] != 0)
{
odd += (frequency[i] & 1);
eve += frequency[i] / 2;
}
}
// Print the result
if (eve >= odd)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver Code
public static void Main(string[] args)
{
string S = "aaabbbccc";
canSplit(S);
}
}
// This code is contributed by AnkThon
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live