📌  相关文章
📜  从给定的字母数中最大化长度为3的回文字符串

📅  最后修改于: 2021-05-19 18:29:54             🧑  作者: Mango

给定大小为26的数组arr [] ,表示字符‘a’到’z’的频率,任务是找到可以从指定的字母数生成的最大长度为3的回文字符串。

例子:

方法:可以根据以下观察结果解决给定问题:

  1. 字符串的数量可以通过首先制造可能的字符串仅一种类型的字符的被最大化。
  2. 之后,取一个类型的两个字符和另一种类型的一个字符组成一个字符串。
  3. 如果仍然存在出现的字符多于1个,则在这些字符取3个,并形成2个类型为“ ACA”和“ BCB”的字符串。
  4. 如果又剩下两个字符,发生了两次,则组成一个字符串,再添加一个以回答。
  5. 在此之后,只出现一次的字符或发生两次字符将被留下。

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

  • 初始化变量res ,将其设为0,以存储可以形成的大小为3的最大回文字符串的计数。
  • 0初始化变量C1C2 ,以分别存储频率为1和2的字符计数
  • 迭代阵列之上,编曲[]和通过ARR递增RES [I] / 3,ARR [I]Arr [I]%3,和增量C11如果ARR [I]%31,否则,如果arr [i]%32,则将C21
  • 遍历后,将res增加min(C1,C2)并更新C1C2 。然后,将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)