Given a string str, the task is to print the maximum count of characters which are greater than both its left and right character in any permutation of the string.
Input: str = “abc”
Permutations of the string with the count of maximal character in each string are:
abc – 0
acb – 1 Here a < c > b
bac – 0
bca – 1 Here b < c > a
cab – 0
cba – 0
Input: str = “geeks”
The string will be “egesk”
- If the string’ length is less than 3 then the answer will be 0 because no permutation is possible which satisfies the given condition.
- If the length of the given string is greater than or equal to 3 then assume that in the resulting string every other character is maximal character, that is there is exactly one character between any two consecutive maximal characters (otherwise we can remove all but the lowest one and add them to the end of the string).
- Assume for simplicity that this number is odd. Then, ideally, the string can have maximal characters in even positions i.e.at most (n-1)/2, where n is the length of the given string while the rest of the remaining characters in odd positions.
- First arrange all the characters in ascending order, place the first half characters at odd positions, and then fill the remaining even positions with the rest of the characters.
In this way, all the characters in even positions will be those characters from which character at the left and the right position are smaller if there is no frequency of smaller character that is too high, start placing a character from an odd position, continue with the same character to even positions, and eventually reach a position next to the odd position from which we started to place the character. Here if the frequency of some smaller character in the string is too high then the count of maximal character will always be less than (n-1)/2.
- Calculate the frequency of each character in the given string.
- Check the character which has the maximum frequency.
- If the maximum frequency element is the smallest element in the given string then mark the flag as 0 otherwise mark the value of flag equal to 1.
- The answer will the minimum of ((n – 1) / 2, n – max_freq – flag).
Below is the implementation of the above approach:
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- String with k distinct characters and no same characters adjacent
- String with maximum number of unique characters
- Maximum number of characters between any two same character in a string
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- C program to swap adjacent characters of a String
- Minimum replacements in a string to make adjacent characters unequal
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Min flips of continuous characters to make all characters same in a string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Replace minimal number of characters to make all characters pair wise distinct
- Maximum repeated frequency of characters in a given string
- Maximum length of balanced string after swapping and removal of characters
- Length of the smallest sub-string consisting of maximum distinct characters
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimize number of unique characters in string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.