📜  字符串的排列,最大字符数大于其相邻字符

📅  最后修改于: 2021-06-26 16:56:07             🧑  作者: Mango

给定一个字符串str,任务是打印最大数目的字符,该最大数目大于字符串的任何排列中其左字符和右字符。
例子:

观察结果:

  • 如果字符串的长度小于3,则答案将为0,因为不可能满足给定条件的排列。
  • 如果给定的字符串的长度大于或等于3,那么假定得到的字符串中的每个其他字符是最大的字符,即在任意两个连续的最大字符之间的一个字符(否则就删除所有,但最低并将其添加到字符串的末尾)。
  • 为简单起见,假设此数字为奇数。然后,理想情况下,字符串可以在偶数位置(即最多(n-1)/ 2)处具有最大字符,其中n是给定字符串的长度,而其余字符在奇数位置。
  • 首先按升序排列所有字符,将前半个字符放在奇数位置,然后用其余字符填充其余的偶数位置。
    通过这种方式,在偶数位置的所有字符将这些字符从字符在左边和右边的位置是小,如果没有小字符的频率太高,开始从一个奇怪的位置放置字符,请继续将相同的字符为偶数位置,并最终到达我们开始放置该字符的奇数位置旁边的位置。在此,如果字符串中某个较小字符的频率太高,则最大字符将始终小于(n-1)/ 2。

方法:

  1. 计算给定字符串中每个字符的频率。
  2. 检查具有最大频率的字符。
  3. 如果最大频率元素是给定字符串的最小元素,则将标志标记为0,否则将标志的值标记为等于1。
  4. 答案将是((n – 1)/ 2,n – max_freq –标志)中的最小值

下面是上述方法的实现:

C++
// C++ program to find maximum count
// of such characters which are greater
// its left and right character in
// any permutation of the string
#include 
using namespace std;
 
// function to find maximum maximal character in the string
int solve(int freq[])
{
    // to store sum of all frequency
    int n = 0;
 
    // to store maximum frequency
    int max_freq = 0;
 
    // frequency of the smallest element
    int first;
 
    // to check if the smallest
    // element have amximum frequqncy or not
    int flag = 0;
 
    // Iterate in the string and count frequency
    for (int i = 0; i < 26; i++) {
        n += freq[i];
 
        // to store frequency of smallest element
        if (freq[i] != 0 && flag == 0) {
            first = freq[i];
            flag = 1;
        }
 
        // to store maximum frequency
        if (max_freq < freq[i])
            max_freq = freq[i];
    }
 
    // if sum of frequency of all element if 0
    if (n == 0)
        return 0;
 
    // if frequency of smallest character
    // if largest frequency
    if (first != max_freq)
        flag = 1;
    else
        flag = 0;
 
    return min((n - 1) / 2, n - max_freq - flag);
}
 
// Function that counts the frequency of
// each element
void solve(string s)
{
    // array to store the frequency of each character
    int freq[26];
 
    // initialize frequency of all character with 0
    memset(freq, 0, sizeof(freq));
 
    // loop to calculate frequqncy of
    // each character in the given string
    for (int i = 0; i < s.length(); i++) {
        freq[s[i] - 'a']++;
    }
 
    cout << solve(freq);
}
 
// Driver Code
int main()
{
    string s = "geeks";
 
    solve(s);
    return 0;
}


Java
// Java program to find maximum count
// of such characters which are greater
// its left and right character in
// any permutation of the string only three characters 
 
class GFG {
 
// function to find maximum maximal character in the string
    static int solve(int freq[]) {
        // to store sum of all frequency
        int n = 0;
 
        // to store maximum frequency
        int max_freq = 0;
 
        // frequency of the smallest element
        int first = 0;
 
        // to check if the smallest
        // element have amximum frequqncy or not
        int flag = 0;
 
        // Iterate in the string and count frequency
        for (int i = 0; i < 26; i++) {
            n += freq[i];
 
            // to store frequency of smallest element
            if (freq[i] != 0 && flag == 0) {
                first = freq[i];
                flag = 1;
            }
 
            // to store maximum frequency
            if (max_freq < freq[i]) {
                max_freq = freq[i];
            }
        }
 
        // if sum of frequency of all element if 0
        if (n == 0) {
            return 0;
        }
 
        // if frequency of smallest character
        // if largest frequency
        if (first != max_freq) {
            flag = 1;
        } else {
            flag = 0;
        }
 
        return Math.min((n - 1) / 2, n - max_freq - flag);
    }
 
// Function that counts the frequency of
// each element
    static void solve(String s) {
        // array to store the frequency of each character
        int freq[] = new int[26];
 
        // loop to calculate frequqncy of
        // each character in the given string
        for (int i = 0; i < s.length(); i++) {
            freq[s.charAt(i) - 'a']++;
        }
 
        System.out.println(solve(freq));
    }
 
// Driver Code
    public static void main(String[] args) {
        String s = "geeks";
 
        solve(s);
 
    }
}
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find maximum
# count of such characters which
# are greater its left and right
# character in any permutation
# of the string
 
# function to find maximum maximal
# character in the string
def Solve_1(freq):
 
    # to store sum of all frequency
    n = 0
 
    # to store maximum frequency
    max_freq = 0
 
    # to check if the smallest
    # element have amximum
    # frequqncy or not
    flag = 0
 
    # Iterate in the string
    # and count frequency
    for i in range(26) :
        n += freq[i]
 
        # to store frequency of
        # smallest element
        if (freq[i] != 0 and flag == 0) :
            first = freq[i]
            flag = 1
 
        # to store maximum frequency
        if (max_freq < freq[i]):
            max_freq = freq[i]
 
    # if sum of frequency of
    # all element if 0
    if (n == 0):
        return 0
 
    # if frequency of smallest character
    # if largest frequency
    if (first != max_freq):
        flag = 1
    else:
        flag = 0
 
    return min((n - 1) // 2, n - max_freq - flag)
 
# Function that counts the
# frequency of each element
def solve(s):
 
    # array to store the frequency
    # of each character initialize
    # frequency of all character with 0
    freq = [0] * 26
 
    # loop to calculate frequqncy of
    # each character in the given string
    for i in range(len(s)):
 
        freq[ord(s[i]) - ord('a')] += 1
 
    print(Solve_1(freq))
 
# Driver Code
if __name__ == "__main__":
    s = "geeks"
    solve(s)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find maximum count
// of such characters which are greater
// its left and right character in
// any permutation of the string only three characters
using System;
public class GFG{
//JAVA program to find maximum count
// of such characters which are greater
// its left and right character in
// any permutation of the string only three characters 
 
 
// function to find maximum maximal character in the string
    static int solve(int []freq) {
        // to store sum of all frequency
        int n = 0;
 
        // to store maximum frequency
        int max_freq = 0;
 
        // frequency of the smallest element
        int first = 0;
 
        // to check if the smallest
        // element have amximum frequqncy or not
        int flag = 0;
 
        // Iterate in the string and count frequency
        for (int i = 0; i < 26; i++) {
            n += freq[i];
 
            // to store frequency of smallest element
            if (freq[i] != 0 && flag == 0) {
                first = freq[i];
                flag = 1;
            }
 
            // to store maximum frequency
            if (max_freq < freq[i]) {
                max_freq = freq[i];
            }
        }
 
        // if sum of frequency of all element if 0
        if (n == 0) {
            return 0;
        }
 
        // if frequency of smallest character
        // if largest frequency
        if (first != max_freq) {
            flag = 1;
        } else {
            flag = 0;
        }
 
        return Math.Min((n - 1) / 2, n - max_freq - flag);
    }
 
// Function that counts the frequency of
// each element
    static void solve(String s) {
        // array to store the frequency of each character
        int []freq = new int[26];
 
        // loop to calculate frequqncy of
        // each character in the given string
        for (int i = 0; i < s.Length; i++) {
            freq[s[i] - 'a']++;
        }
 
        Console.Write(solve(freq));
    }
 
// Driver Code
    public static void Main() {
        String s = "geeks";
 
        solve(s);
 
    }
}
// This code is contributed by Rajput-JI


PHP


Javascript


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。