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.
Implementation:
C++
#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] << " " ;
}
int main()
{
string str = "abcd" ;
printGreaterCount(str);
return 0;
}
|
Java
class GFG {
static void printGreaterCount(String str)
{
int len = str.length(), right[] = new int [len];
for ( int i = 0 ; i < len; i++) {
for ( int j = i + 1 ; j < len; j++) {
if (str.charAt(i) < str.charAt(j)) {
right[i]++;
}
}
}
for ( int i = 0 ; i < len; i++) {
System.out.print(right[i] + " " );
}
}
public static void main(String[] args)
{
String str = "abcd" ;
printGreaterCount(str);
}
}
|
Python3
def printGreaterCount( str ):
len__ = len ( str )
right = [ 0 for i in range (len__)]
for i in range (len__):
for j in range (i + 1 , len__, 1 ):
if ( str [i] < str [j]):
right[i] + = 1
for i in range (len__):
print (right[i], end = " " )
if __name__ = = '__main__' :
str = "abcd"
printGreaterCount( str )
|
C#
using System;
public class GFG {
static void printGreaterCount(String str)
{
int len = str.Length;
int [] right = new int [len];
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++) {
Console.Write(right[i] + " " );
}
}
public static void Main()
{
String str = "abcd" ;
printGreaterCount(str);
}
}
|
PHP
<?php
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 ] . " " ;
}
$str = 'abcd' ;
printGreaterCount( $str );
?>
|
Javascript
<script>
function printGreaterCount(str)
{
let len = str.length;
let right = new Array(len);
right.fill(0);
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (str[i].charCodeAt() < str[j].charCodeAt())
{
right[i]++;
}
}
}
for (let i = 0; i < len; i++) {
document.write(right[i] + " " );
}
}
let str = "abcd" ;
printGreaterCount(str);
</script>
|
Time Complexity: O(N * N), where N is the length of the given string.
Auxiliary Space: O(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++
#include <bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26
void printGreaterCount(string str)
{
int len = str.length();
int ans[len] = { 0 }, count[MAX_CHAR] = { 0 };
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] << " " ;
}
int main()
{
string str = "abcd" ;
printGreaterCount(str);
return 0;
}
|
Java
public class GFG {
final static int MAX_CHAR = 26 ;
static void printGreaterCount(String str)
{
int len = str.length();
int ans[] = new int [len], count[] = new int [MAX_CHAR];
for ( int i = len - 1 ; i >= 0 ; i--) {
count[str.charAt(i) - 'a' ]++;
for ( int j = str.charAt(i) - 'a' + 1 ; j < MAX_CHAR; j++) {
ans[i] += count[j];
}
}
for ( int i = 0 ; i < len; i++) {
System.out.print(ans[i] + " " );
}
}
static public void main(String[] args)
{
String str = "abcd" ;
printGreaterCount(str);
}
}
|
Python3
MAX_CHAR = 26 ;
def printGreaterCount(str1):
len1 = len (str1);
ans = [ 0 ] * len1;
count = [ 0 ] * MAX_CHAR;
for i in range (len1 - 1 , - 1 , - 1 ):
count[ ord (str1[i]) - ord ( 'a' )] + = 1 ;
for j in range ( ord (str1[i]) - ord ( 'a' ) + 1 ,MAX_CHAR):
ans[i] + = count[j];
for i in range (len1):
print (ans[i],end = " " );
str1 = "abcd" ;
printGreaterCount(str1);
|
C#
using System;
class GFG {
static int MAX_CHAR = 26;
static void printGreaterCount( string str)
{
int len = str.Length;
int [] ans = new int [len];
int [] count = new int [MAX_CHAR];
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++) {
Console.Write(ans[i] + " " );
}
}
static void Main()
{
string str = "abcd" ;
printGreaterCount(str);
}
}
|
PHP
<?php
$MAX_CHAR =26;
function printGreaterCount( $str )
{
global $MAX_CHAR ;
$len = strlen ( $str );
$ans = array_fill (0, $len , 0);
$count = array_fill (0, $MAX_CHAR , 0);
for ( $i = $len - 1; $i >= 0; $i --)
{
$count [ord( $str [ $i ]) - ord( 'a' )]++;
for ( $j = ord( $str [ $i ]) - ord( 'a' ) + 1; $j < $MAX_CHAR ; $j ++)
$ans [ $i ] += $count [ $j ];
}
for ( $i = 0; $i < $len ; $i ++)
echo $ans [ $i ]. " " ;
}
$str = "abcd" ;
printGreaterCount( $str );
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function printGreaterCount(str)
{
let len = str.length;
let ans = new Array(len);
ans.fill(0);
let count = new Array(MAX_CHAR);
count.fill(0);
for (let i = len - 1; i >= 0; i--) {
count[str[i].charCodeAt() - 'a' .charCodeAt()]++;
for (let j = str[i].charCodeAt() - 'a' .charCodeAt() + 1; j < MAX_CHAR; j++) {
ans[i] += count[j];
}
}
for (let i = 0; i < len; i++) {
document.write(ans[i] + " " );
}
}
let str = "abcd" ;
printGreaterCount(str);
</script>
|
Time Complexity: O(N)
Auxiliary space: O(N)