给定大小为26的数组arr [] ,表示字符‘a’到’z’的频率,任务是找到可以从指定的字母数生成的最大长度为3的回文字符串。
例子:
Input: arr[] = {4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Output: 3
Explanation:One of the possible solution is to take two characters of ‘b’ and one character of ‘a’, making string “bab”. Now, 3 ‘a’ and 3 ‘b’ are left from which strings “aaa” and “bbb” can be made. Thus total of 3 palindromic strings can be formed.
Input: arr[] = {2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Output: 1
Explanation: The only possible palindromic string is “aba”.
方法:可以根据以下观察结果解决给定问题:
- 字符串的数量可以通过首先制造可能的字符串仅一种类型的字符的被最大化。
- 之后,取一个类型的两个字符和另一种类型的一个字符组成一个字符串。
- 如果仍然存在出现的字符多于1个,则在这些字符取3个,并形成2个类型为“ ACA”和“ BCB”的字符串。
- 如果又剩下两个字符,发生了两次,则组成一个字符串,再添加一个以回答。
- 在此之后,只出现一次的字符或发生两次字符将被留下。
请按照以下步骤解决问题:
- 初始化变量res ,将其设为0,以存储可以形成的大小为3的最大回文字符串的计数。
- 用0初始化变量C1和C2 ,以分别存储频率为1和2的字符计数
- 迭代阵列之上,编曲[]和通过ARR递增RES [I] / 3,组ARR [I]给Arr [I]%3,和增量C1由1如果ARR [I]%3为1,否则,如果arr [i]%3为2,则将C2加1 。
- 遍历后,将res增加min(C1,C2)并更新C1和C2 。然后,将res增加C2 / 3的两倍并更新C2 。最后,将res增加C2 / 2 。
- 最后,打印res作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count maximum number
// of palindromic string of length 3
void maximum_pallindromic(int arr[])
{
// Stores the final count of
// palindromic strings
int res = 0;
int c1 = 0, c2 = 0;
// Traverse the array
for (int i = 0; i < 26; i++) {
// Increment res by arr[i]/3,
// i.e forming string of
// only i +'a' character
res += arr[i] / 3;
// Store remainder
arr[i] = arr[i] % 3;
// Increment c1 by one, if
// current frequency is 1
if (arr[i] == 1)
c1++;
// Increment c2 by one, if
// current frequency is 2
else if (arr[i] == 2)
c2++;
}
// Count palindromic strings of
// length 3 having the character
// at the ends different from that
// present in the middle
res += min(c1, c2);
int t = min(c1, c2);
// Update c1 and c2
c1 -= t;
c2 -= t;
// Increment res by 2 * c2/3
res += 2 * (c2 / 3);
c2 %= 3;
res += c2 / 2;
// Finally print the result
cout << res;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Function Call
maximum_pallindromic(arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count maximum number
// of palindromic String of length 3
static void maximum_pallindromic(int arr[])
{
// Stores the final count of
// palindromic Strings
int res = 0;
int c1 = 0, c2 = 0;
// Traverse the array
for (int i = 0; i < 26; i++)
{
// Increment res by arr[i]/3,
// i.e forming String of
// only i +'a' character
res += arr[i] / 3;
// Store remainder
arr[i] = arr[i] % 3;
// Increment c1 by one, if
// current frequency is 1
if (arr[i] == 1)
c1++;
// Increment c2 by one, if
// current frequency is 2
else if (arr[i] == 2)
c2++;
}
// Count palindromic Strings of
// length 3 having the character
// at the ends different from that
// present in the middle
res += Math.min(c1, c2);
int t = Math.min(c1, c2);
// Update c1 and c2
c1 -= t;
c2 -= t;
// Increment res by 2 * c2/3
res += 2 * (c2 / 3);
c2 %= 3;
res += c2 / 2;
// Finally print the result
System.out.print(res);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Function Call
maximum_pallindromic(arr);
}
}
// This code is contributed by aashish1995
Python3
# Python3 program for the above approach
# Function to count maximum number
# of palindromic string of length 3
def maximum_pallindromic(arr):
# Stores the final count of
# palindromic strings
res = 0
c1 = 0
c2 = 0
# Traverse the array
for i in range(26):
# Increment res by arr[i]/3,
# i.e forming string of
# only i +'a' character
res += arr[i] // 3
# Store remainder
arr[i] = arr[i] % 3
# Increment c1 by one, if
# current frequency is 1
if (arr[i] == 1):
c1 += 1
# Increment c2 by one, if
# current frequency is 2
elif (arr[i] == 2):
c2 += 1
# Count palindromic strings of
# length 3 having the character
# at the ends different from that
# present in the middle
res += min(c1, c2)
t = min(c1, c2)
# Update c1 and c2
c1 -= t
c2 -= t
# Increment res by 2 * c2/3
res += 2 * (c2 // 3)
c2 %= 3
res += c2 // 2
# Finally print the result
print(res)
# Driver Code
if __name__ == "__main__":
# Given array
arr = [ 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 ]
# Function Call
maximum_pallindromic(arr)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count maximum number
// of palindromic String of length 3
static void maximum_pallindromic(int []arr)
{
// Stores the readonly count of
// palindromic Strings
int res = 0;
int c1 = 0, c2 = 0;
// Traverse the array
for (int i = 0; i < 26; i++)
{
// Increment res by arr[i]/3,
// i.e forming String of
// only i +'a' character
res += arr[i] / 3;
// Store remainder
arr[i] = arr[i] % 3;
// Increment c1 by one, if
// current frequency is 1
if (arr[i] == 1)
c1++;
// Increment c2 by one, if
// current frequency is 2
else if (arr[i] == 2)
c2++;
}
// Count palindromic Strings of
// length 3 having the character
// at the ends different from that
// present in the middle
res += Math.Min(c1, c2);
int t = Math.Min(c1, c2);
// Update c1 and c2
c1 -= t;
c2 -= t;
// Increment res by 2 * c2/3
res += 2 * (c2 / 3);
c2 %= 3;
res += c2 / 2;
// Finally print the result
Console.Write(res);
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Function Call
maximum_pallindromic(arr);
}
}
// This code is contributed by 29AjayKumar
3
时间复杂度: O(1)
辅助空间: O(1)