Number of Larger Elements on right side in a string

Given a string, find count of number of larger alphabets for every character of the string.

Examples:

Input  : str = "abcd"
Output : 3 2 1 0
There are 3 greater characters on right of 'a',
2 greater characters on right of 'b', 1 greater
character on right of 'c' and 0 greater characters
on right of 'd'.
      
Input :  geeks
Output : 2 2 2 1 0


A naive approach is to use two for loops. First will keep track of each alphabet in string and second loop will be used to find no of larger alphabet according to ASCII values.

C++

// CPP program to find counts of right greater
// characters for every character.
#include <bits/stdc++.h>
using namespace std;

void printGreaterCount(string str)
{
    int len = str.length(), right[len] = { 0 };
    for (int i = 0; i < len; i++) 
        for (int j = i + 1; j < len; j++) 
            if (str[i] < str[j])
                right[i]++;

    for (int i = 0; i < len; i++)
        cout << right[i] << " ";
}

// Driver code
int main()
{
    string str = "abcd";
    printGreaterCount(str);
    return 0;
}

PHP

<?php
// PHP program to find counts 
// of right greater characters
// for every character.

function printGreaterCount($str)
{
    $len = strlen($str); 
    $right = array_fill(0, $len, 0);
    for ($i = 0; $i < $len; $i++) 
    {
        for ($j = $i + 1; $j < $len; $j++) 
            if ($str[$i] < $str[$j])
                $right[$i]++;
    }

    for ($i = 0; $i < $len; $i++)
        echo $right[$i] . " ";
}

// Driver code
$str = 'abcd';
printGreaterCount($str);

// This code is contributed 
// by Abhinav96
?>


Output:

3 2 1 0

Time Complexity : O(N * N)

An efficient approach is to traverse the string from right and keep track of counts of characters from right side. For every character that we traverse from right, we increment its count in count array and add counts of all greater characters to answer for this character.

C++

// C++ program to count greater characters on right
// side of every character.
#include <bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26

void printGreaterCount(string str)
{
    int len = str.length();
 
    // Arrays to store result and character counts.
    int ans[len] = { 0 }, count[MAX_CHAR] = { 0 };

    // start from right side of string
    for (int i = len - 1; i >= 0; i--) 
    {
        count[str[i] - 'a']++;
        for (int j = str[i] - 'a' + 1; j < MAX_CHAR; j++)
            ans[i] += count[j];
    }

    for (int i = 0; i < len; i++)
        cout << ans[i] << " ";
}

// Driver code
int main()
{
    string str = "abcd";
    printGreaterCount(str);
    return 0;
}
Output:

3 2 1 0

Time Complexity : O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



2.1 Average Difficulty : 2.1/5.0
Based on 8 vote(s)






User Actions