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.
Examples:
Input: str = “abc”
Output: 1
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”
Output: 2
The string will be “egesk”
Observations:
- 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.
Approach:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int freq[])
{
int n = 0;
int max_freq = 0;
int first;
int flag = 0;
for ( int i = 0; i < 26; i++) {
n += freq[i];
if (freq[i] != 0 && flag == 0) {
first = freq[i];
flag = 1;
}
if (max_freq < freq[i])
max_freq = freq[i];
}
if (n == 0)
return 0;
if (first != max_freq)
flag = 1;
else
flag = 0;
return min((n - 1) / 2, n - max_freq - flag);
}
void solve(string s)
{
int freq[26];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < s.length(); i++) {
freq[s[i] - 'a' ]++;
}
cout << solve(freq);
}
int main()
{
string s = "geeks" ;
solve(s);
return 0;
}
|
Java
class GFG {
static int solve( int freq[]) {
int n = 0 ;
int max_freq = 0 ;
int first = 0 ;
int flag = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
n += freq[i];
if (freq[i] != 0 && flag == 0 ) {
first = freq[i];
flag = 1 ;
}
if (max_freq < freq[i]) {
max_freq = freq[i];
}
}
if (n == 0 ) {
return 0 ;
}
if (first != max_freq) {
flag = 1 ;
} else {
flag = 0 ;
}
return Math.min((n - 1 ) / 2 , n - max_freq - flag);
}
static void solve(String s) {
int freq[] = new int [ 26 ];
for ( int i = 0 ; i < s.length(); i++) {
freq[s.charAt(i) - 'a' ]++;
}
System.out.println(solve(freq));
}
public static void main(String[] args) {
String s = "geeks" ;
solve(s);
}
}
|
Python3
def Solve_1(freq):
n = 0
max_freq = 0
flag = 0
for i in range ( 26 ) :
n + = freq[i]
if (freq[i] ! = 0 and flag = = 0 ) :
first = freq[i]
flag = 1
if (max_freq < freq[i]):
max_freq = freq[i]
if (n = = 0 ):
return 0
if (first ! = max_freq):
flag = 1
else :
flag = 0
return min ((n - 1 ) / / 2 , n - max_freq - flag)
def solve(s):
freq = [ 0 ] * 26
for i in range ( len (s)):
freq[ ord (s[i]) - ord ( 'a' )] + = 1
print (Solve_1(freq))
if __name__ = = "__main__" :
s = "geeks"
solve(s)
|
C#
using System;
public class GFG{
static int solve( int []freq) {
int n = 0;
int max_freq = 0;
int first = 0;
int flag = 0;
for ( int i = 0; i < 26; i++) {
n += freq[i];
if (freq[i] != 0 && flag == 0) {
first = freq[i];
flag = 1;
}
if (max_freq < freq[i]) {
max_freq = freq[i];
}
}
if (n == 0) {
return 0;
}
if (first != max_freq) {
flag = 1;
} else {
flag = 0;
}
return Math.Min((n - 1) / 2, n - max_freq - flag);
}
static void solve(String s) {
int []freq = new int [26];
for ( int i = 0; i < s.Length; i++) {
freq[s[i] - 'a' ]++;
}
Console.Write(solve(freq));
}
public static void Main() {
String s = "geeks" ;
solve(s);
}
}
|
PHP
<?php
function Solve_1( $freq )
{
$n = 0;
$max_freq = 0;
$flag = 0;
for ( $i = 0; $i < 26; $i ++)
{
$n += $freq [ $i ];
if ( $freq [ $i ] != 0 and $flag == 0)
{
$first = $freq [ $i ];
$flag = 1;
}
if ( $max_freq < $freq [ $i ])
$max_freq = $freq [ $i ];
}
if ( $n == 0)
return 0;
if ( $first != $max_freq )
$flag = 1;
else
$flag = 0;
return min(( $n - 1) / 2,
$n - $max_freq - $flag );
}
function solve( $s )
{
$freq = array_fill (0, 26, 0);
for ( $i = 0; $i < strlen ( $s ); $i ++)
$freq [ord( $s [ $i ]) - ord( 'a' )] += 1;
print (Solve_1( $freq ));
}
$s = "geeks" ;
solve( $s );
?>
|
Javascript
<script>
function solve1(freq)
{
let n = 0;
let max_freq = 0;
let first = 0;
let flag = 0;
for (let i = 0; i < 26; i++) {
n += freq[i];
if (freq[i] != 0 && flag == 0) {
first = freq[i];
flag = 1;
}
if (max_freq < freq[i]) {
max_freq = freq[i];
}
}
if (n == 0) {
return 0;
}
if (first != max_freq) {
flag = 1;
} else {
flag = 0;
}
return Math.min(Math.floor((n - 1) / 2), n - max_freq - flag);
}
function solve(s)
{
let freq = new Array(26);
for (let i=0;i<26;i++)
{
freq[i]=0;
}
for (let i = 0; i < s.length; i++) {
freq[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
document.write(solve1(freq));
}
let s = "geeks" ;
solve(s);
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n is the size of the given string
- Auxiliary Space: O(26)