Group all occurrences of characters according to first appearance
Last Updated :
11 Sep, 2023
Given a string of lowercase characters, the task is to print the string in a manner such that a character comes first in string displays first with all its occurrences in string.
Examples:
Input : str = "geeksforgeeks"
Output: ggeeeekkssfor
Explanation: In the given string 'g' comes first
and occurs 2 times so it is printed first
Then 'e' comes in this string and 4 times so
it gets printed. Similarly remaining string is
printed.
Input : str = "occurrence"
output : occcurreen
Input : str = "cdab"
Output : cdab
This problem is a string version of following problem for array of integers. Group multiple occurrence of array elements ordered by first occurrences Since given strings have only 26 possible characters, it is easier to implement for strings.
Implementation:
- Count the occurrence of all the characters in given string using an array of size 26.
- Then start traversing the string. Print every character its count times.
C++
# include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void printGrouped(string str)
{
int n = str.length();
int count[MAX_CHAR] = {0};
for ( int i = 0 ; i < n ; i++)
count[str[i]- 'a' ]++;
for ( int i = 0; i < n ; i++)
{
while (count[str[i]- 'a' ]--)
cout << str[i];
count[str[i]- 'a' ] = 0;
}
}
int main()
{
string str = "geeksforgeeks" ;
printGrouped(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int MAX_CHAR = 26 ;
static void printGrouped(String str)
{
int n = str.length();
int count[] = new int [MAX_CHAR];
Arrays.fill(count, 0 );
for ( int i = 0 ; i < n ; i++)
count[str.charAt(i)- 'a' ]++;
for ( int i = 0 ; i < n ; i++)
{
while (count[str.charAt(i)- 'a' ]--> 0 )
System.out.print(str.charAt(i));
count[str.charAt(i)- 'a' ] = 0 ;
}
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
printGrouped(str);
}
}
|
Python3
MAX_CHAR = 26
def printGrouped(string):
n = len (string)
count = [ 0 ] * MAX_CHAR
for i in range (n):
count[ ord (string[i]) - ord ( "a" )] + = 1
for i in range (n):
while count[ ord (string[i]) - ord ( "a" )]:
print (string[i], end = "")
count[ ord (string[i]) - ord ( "a" )] - = 1
count[ ord (string[i]) - ord ( "a" )] = 0
if __name__ = = "__main__" :
string = "geeksforgeeks"
printGrouped(string)
|
C#
using System;
class GFG
{
static int MAX_CHAR = 26;
static void printGrouped(String str)
{
int n = str.Length;
int []count = new int [MAX_CHAR];
for ( int i = 0 ; i < n ; i++)
count[str[i] - 'a' ]++;
for ( int i = 0; i < n ; i++)
{
while (count[str[i] - 'a' ] != 0)
{
Console.Write(str[i]);
count[str[i] - 'a' ]--;
}
count[str[i] - 'a' ] = 0;
}
}
public static void Main()
{
string str = "geeksforgeeks" ;
printGrouped(str);
}
}
|
Javascript
var MAX_CHAR = 26;
function printGrouped(str)
{
var n = str.length;
var count = Array(MAX_CHAR).fill(0);
for ( var i=0; i < n; i++)
{
count[str.charAt(i).charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
for ( var i=0; i < n; i++)
{
while (count[str.charAt(i).charCodeAt(0) - 'a' .charCodeAt(0)]-- > 0)
{
console.log(str.charAt(i));
}
count[str.charAt(i).charCodeAt(0) - 'a'.charCodeAt(0)] = 0;
}
}
var str = "geeksforgeeks" ;
printGrouped(str);
|
Time complexity : O(n)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...