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

📅  最后修改于: 2021-09-05 10:56:49             🧑  作者: Mango

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

例子:

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

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

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

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


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live