Count of lexicographically smaller characters on right
Given a string consisting of only lowercase English alphabets. The task is to count the total number of alphabetically smaller characters on the right side of characters at each index.
Examples:
Input: str = “edcba”
Output: 4 3 2 1 0
Explanation:
The number of characters on the right side of index 0 which is smaller than
e are dcba = 4
The number of characters on the right side of index 1 which is smaller than
d are cba = 3
The number of characters on the right side of index 2 which is smaller than
c are ba = 2
The number of characters on the right side of index 3 which is smaller than
b are a = 1
The number of characters on the right side of index 4 which is smaller than
a are ‘\0’ = 0
Input: str = “eaaa”
Output: 3 0 0 0
Naive Approach:
The idea is to traverse all the characters on the right side of each index of string one by one and print the count of characters that are alphabetically smaller.
C++
#include <bits/stdc++.h>
using namespace std;
void countSmaller(string str)
{
int n = str.length();
for ( int i = 0; i < n; i++) {
int cnt = 0;
for ( int j = i + 1; j < n; j++) {
if (str[j] < str[i]) {
cnt += 1;
}
}
cout << cnt << " " ;
}
}
int main()
{
string str = "edcba" ;
countSmaller(str);
}
|
Java
class GFG
{
static void countSmaller(String str)
{
int n = str.length();
for ( int i = 0 ; i < n; i++)
{
int cnt = 0 ;
for ( int j = i + 1 ; j < n; j++)
{
if (str.charAt(j) < str.charAt(i))
{
cnt += 1 ;
}
}
System.out.print(cnt+ " " );
}
}
public static void main(String[] args)
{
String str = "edcba" ;
countSmaller(str);
}
}
|
Python3
def countSmaller( str ):
n = len ( str );
for i in range (n):
cnt = 0 ;
for j in range (i + 1 , n):
if ( str [j] < str [i]):
cnt + = 1 ;
print (cnt, end = " " );
if __name__ = = '__main__' :
str = "edcba" ;
countSmaller( str );
|
C#
using System;
class GFG
{
static void countSmaller(String str)
{
int n = str.Length;
for ( int i = 0; i < n; i++)
{
int cnt = 0;
for ( int j = i + 1; j < n; j++)
{
if (str[j] < str[i])
{
cnt += 1;
}
}
Console.Write(cnt+ " " );
}
}
public static void Main(String[] args)
{
String str = "edcba" ;
countSmaller(str);
}
}
|
Javascript
<script>
function countSmaller(str)
{
var n = str.length;
for ( var i = 0; i < n; i++) {
var cnt = 0;
for ( var j = i + 1; j < n; j++) {
if (str[j] < str[i]) {
cnt += 1;
}
}
document.write( cnt + " " );
}
}
var str = "edcba" ;
countSmaller(str);
</script>
|
Time Complexity: O(N2), where N = length of string
Auxiliary Space: O(1)
Better Approach: The idea is to Use Self-Balancing BST.
A Self-Balancing Binary Search Tree (AVL, Red Black, .. etc) can be used to get the solution in O(N log N) time complexity. We can augment these trees so that every node N contains the size of the subtree rooted with N. We have used the AVL tree in the following implementation.
We traverse the string from right to left and insert all elements one by one in an AVL tree. While inserting a new key in an AVL tree, we first compare the key with the root. If the key is greater than the root, then it is greater than all the nodes in the left subtree of the root. So we add the size of the left subtree to the count of smaller elements for the key being inserted. We recursively follow the same approach for all nodes down the root.
Please refer to this article for the implementation of the above approach.
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Efficient Approach:
The idea is to use hashing techniques because the string consists of only lowercase alphabets. So here we can take an array of size 26 that is used to store the count of smaller characters on the right side of that index.
Traverse the array from the right and keep updating the count of characters in the hash array. Now, to find the count of smaller characters on the right, we can simply traverse the hash array of size 26 every time to count smaller characters encountered so far.
C++
#include <bits/stdc++.h>
using namespace std;
void countSmaller(string str)
{
int n = str.length();
int arr[26] = { 0 };
int ans[n];
for ( int i = n - 1; i >= 0; i--) {
arr[str[i] - 'a' ]++;
int ct = 0;
for ( int j = 0; j < str[i] - 'a' ; j++) {
ct += arr[j];
}
ans[i] = ct;
}
for ( int i = 0; i < n; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
string str = "edcbaa" ;
countSmaller(str);
return 0;
}
|
Java
class GFG
{
static void countSmaller(String str)
{
int n = str.length();
int arr[] = new int [ 26 ];
int ans[] = new int [n];
for ( int i = n - 1 ; i >= 0 ; i--)
{
arr[str.charAt(i) - 'a' ]++;
int ct = 0 ;
for ( int j = 0 ; j < str.charAt(i) - 'a' ; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(ans[i] + " " );
}
}
public static void main(String[] args)
{
String str = "edcbaa" ;
countSmaller(str);
}
}
|
Python3
def countSmaller( str ):
n = len ( str );
arr = [ 0 ] * 26 ;
ans = [ 0 ] * n;
for i in range (n - 1 , - 1 , - 1 ):
arr[ ord ( str [i] ) - ord ( 'a' )] + = 1 ;
ct = 0 ;
for j in range ( ord ( str [i] ) - ord ( 'a' )):
ct + = arr[j];
ans[i] = ct;
for i in range (n):
print (ans[i], end = " " );
if __name__ = = '__main__' :
str = "edcbaa" ;
countSmaller( str );
|
C#
using System;
class GFG
{
static void countSmaller(String str)
{
int n = str.Length;
int []arr = new int [26];
int []ans = new int [n];
for ( int i = n - 1; i >= 0; i--)
{
arr[str[i] - 'a' ]++;
int ct = 0;
for ( int j = 0; j < str[i] - 'a' ; j++)
{
ct += arr[j];
}
ans[i] = ct;
}
for ( int i = 0; i < n; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main(String[] args)
{
String str = "edcbaa" ;
countSmaller(str);
}
}
|
Javascript
<script>
function countSmaller(str)
{
var n = str.length;
var arr = Array(26).fill(0);
var ans = Array(n);
for ( var i = n - 1; i >= 0; i--) {
arr[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
var ct = 0;
for ( var j = 0; j < str[i].charCodeAt(0) - 'a' .charCodeAt(0); j++) {
ct += arr[j];
}
ans[i] = ct;
}
for ( var i = 0; i < n; i++) {
document.write( ans[i] + " " );
}
}
var str = "edcbaa" ;
countSmaller(str);
</script>
|
Time Complexity: O(N*26)
Auxiliary Space: O(N+26)
Last Updated :
28 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...