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; } |
Java
// Java program to find counts of right greater // characters for every character. 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] + " " ); } } // Driver code public static void main(String[] args) { String str = "abcd" ; printGreaterCount(str); } } // This code is contributed Rajput-Ji |
Python3
# Python3 program to find counts of right
# greater characters for every character.
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 = " ")
# Driver code
if __name__ == '__main__':
str = "abcd"
printGreaterCount(str)
# This code is contributed by
# Sahil_Shelangia
[tabby title="C#"]
// C# program to find counts of right greater // characters for every character. 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] + " " ); } } // Driver code public static void Main() { String str = "abcd" ; printGreaterCount(str); } } // This code is contributed by 29AjayKumar |
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 ?> |
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; } |
Java
// Java program to count greater characters on right // side of every character. public class GFG { final static int MAX_CHAR = 26 ; static void printGreaterCount(String str) { int len = str.length(); // Arrays to store result and character counts. int ans[] = new int [len], count[] = new int [MAX_CHAR]; // start from right side of string 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] + " " ); } } // Driver code static public void main(String[] args) { String str = "abcd" ; printGreaterCount(str); } } |
3 2 1 0
Time Complexity : O(N)
Recommended Posts:
- Number of elements that can be seen from right side
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Minimum swaps to group similar characters side by side?
- Number of squares of side length required to cover an N*M rectangle
- Minimum number of points to be removed to get remaining points on one side of axis
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Count number of elements between two given elements in array
- Minimum number of given operations required to convert a string to another string
- Number of sub-strings which are anagram of any sub-string of another string
- Maximum number of contiguous array elements with same number of set bits
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Find the nearest smaller numbers on left side in an array
- Sum of elements of all partitions of number such that no element is less than K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.