Group consecutive characters of same type in a string
Last Updated :
15 Dec, 2022
Given a string str consisting of upper case alphabets, numeric digits and arithmetic operators, the task is to group them into continuous sub-strings of the same type.
Examples:
Input: str = “G E E K S 1 2 3 4 5”
Output: GEEKS 12345
All contiguous upper case characters can be grouped together
and all the numeric characters can be grouped together in a separate group.
Input: str = “DEGFT +- * 5 6 7”
Output: DEGFT +-* 567
Approach: Remove all the spaces from string. After removing all the white spaces, traverse the string one by one and group the characters according to their ASCII values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string groupCharacters(string s, int len)
{
string temp = "" ;
for ( int i = 0; i < len; i++)
if (s[i] != ' ' )
temp = temp + s[i];
len = temp.length();
string ans = "" ;
int i = 0;
while (i < len) {
if ( int (temp[i]) >= int ( 'A' )
&& int (temp[i]) <= int ( 'Z' )) {
while (i < len && int (temp[i]) >= int ( 'A' )
&& int (temp[i]) <= int ( 'Z' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else if ( int (temp[i]) >= int ( '0' )
&& int (temp[i]) <= int ( '9' )) {
while (i < len && int (temp[i]) >= int ( '0' )
&& int (temp[i]) <= int ( '9' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else {
while (i < len && int (temp[i]) >= int ( '*' )
&& int (temp[i]) <= int ( '/' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
}
return ans;
}
int main()
{
string s = "34FTG234+ +- *" ;
int len = s.length();
cout << groupCharacters(s, len);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String groupCharacters( char [] s, int len)
{
String temp = "" ;
for ( int i = 0 ; i < len; i++)
if (s[i] != ' ' )
temp = temp + s[i];
len = temp.length();
String ans = "" ;
int i = 0 ;
while (i < len) {
if (temp.charAt(i) >= ( 'A' )
&& temp.charAt(i) <= ( 'Z' )) {
while (i < len && temp.charAt(i) >= ( 'A' )
&& temp.charAt(i) <= ( 'Z' )) {
ans = ans + temp.charAt(i);
i++;
}
ans = ans + " " ;
}
else if (temp.charAt(i) >= ( '0' )
&& temp.charAt(i) <= ( '9' )) {
while (i < len && temp.charAt(i) >= ( '0' )
&& temp.charAt(i) <= ( '9' )) {
ans = ans + temp.charAt(i);
i++;
}
ans = ans + " " ;
}
else {
while (i < len && temp.charAt(i) >= ( '*' )
&& temp.charAt(i) <= ( '/' )) {
ans = ans + temp.charAt(i);
i++;
}
ans = ans + " " ;
}
}
return ans;
}
public static void main(String[] args)
{
String s = "34FTG234+ +- *" ;
int len = s.length();
System.out.print(
groupCharacters(s.toCharArray(), len));
}
}
|
Python 3
def groupCharacters(s, l):
temp = ""
for i in range (l):
if (s[i] ! = ' ' ):
temp = temp + s[i]
l = len (temp)
ans = ""
i = 0
while (i < l):
if ( ord (temp[i]) > = ord ( 'A' )
and ord (temp[i]) < = ord ( 'Z' )):
while (i < l and ord (temp[i]) > = ord ( 'A' )
and ord (temp[i]) < = ord ( 'Z' )):
ans = ans + temp[i]
i + = 1
ans = ans + " "
elif ( ord (temp[i]) > = ord ( '0' )
and ord (temp[i]) < = ord ( '9' )):
while (i < l and ord (temp[i]) > = ord ( '0' )
and ord (temp[i]) < = ord ( '9' )):
ans = ans + temp[i]
i + = 1
ans = ans + " "
else :
while (i < l and ord (temp[i]) > = ord ( '*' )
and ord (temp[i]) < = ord ( '/' )):
ans = ans + temp[i]
i + = 1
ans = ans + " "
return ans
if __name__ = = "__main__" :
s = "34FTG234+ +- *"
l = len (s)
print (groupCharacters(s, l))
|
C#
using System;
class GFG {
static String groupCharacters( char [] s, int len)
{
string temp = "" ;
for ( int j = 0; j < len; j++)
if (s[j] != ' ' )
temp = temp + s[j];
len = temp.Length;
string ans = "" ;
int i = 0;
while (i < len) {
if (temp[i] >= ( 'A' ) && temp[i] <= ( 'Z' )) {
while (i < len && temp[i] >= ( 'A' )
&& temp[i] <= ( 'Z' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else if (temp[i] >= ( '0' ) && temp[i] <= ( '9' )) {
while (i < len && temp[i] >= ( '0' )
&& temp[i] <= ( '9' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else {
while (i < len && temp[i] >= ( '*' )
&& temp[i] <= ( '/' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
}
return ans;
}
public static void Main()
{
string s = "34FTG234+ +- *" ;
int len = s.Length;
Console.WriteLine(
groupCharacters(s.ToCharArray(), len));
}
}
|
PHP
<?php
function groupCharacters( $s , $len )
{
$temp = "" ;
for ( $i = 0; $i < $len ; $i ++)
if ( $s [ $i ] != ' ' )
$temp = $temp . $s [ $i ];
$len = strlen ( $temp );
$ans = "" ;
$i = 0;
while ( $i < $len )
{
if (ord( $temp [ $i ]) >= ord( 'A' ) &&
ord( $temp [ $i ]) <= ord( 'Z' ))
{
while ( $i < $len && ord( $temp [ $i ]) >= ord( 'A' ) &&
ord( $temp [ $i ]) <= ord( 'Z' ))
{
$ans = $ans . $temp [ $i ];
$i ++;
}
$ans = $ans . " " ;
}
else if (ord( $temp [ $i ]) >= ord( '0' ) &&
ord( $temp [ $i ]) <= ord( '9' ))
{
while ( $i < $len && ord( $temp [ $i ]) >= ord( '0' ) &&
ord( $temp [ $i ]) <= ord( '9' ))
{
$ans = $ans . $temp [ $i ];
$i ++;
}
$ans = $ans . " " ;
}
else
{
while ( $i < $len && ord( $temp [ $i ]) >= ord( '*' ) &&
ord( $temp [ $i ]) <= ord( '/' ))
{
$ans = $ans . $temp [ $i ];
$i ++;
}
$ans = $ans . " " ;
}
}
return $ans ;
}
$s = "34FTG234+ +- *" ;
$len = strlen ( $s );
print (groupCharacters( $s , $len ));
?>
|
Javascript
<script>
function groupCharacters(s,len)
{
let temp = "" ;
for (let i = 0; i < len; i++)
if (s[i] != ' ' )
temp = temp + s[i];
len = temp.length;
let ans = "" ;
let i = 0;
while (i < len) {
if (temp[i] >= ( 'A' )
&& temp[i] <= ( 'Z' )) {
while (i < len && temp[i] >= ( 'A' )
&& temp[i] <= ( 'Z' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else if (temp[i] >= ( '0' )
&& temp[i] <= ( '9' )) {
while (i < len && temp[i] >= ( '0' )
&& temp[i] <= ( '9' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
else {
while (i < len && temp[i] >= ( '*' )
&& temp[i] <= ( '/' )) {
ans = ans + temp[i];
i++;
}
ans = ans + " " ;
}
}
return ans;
}
let s = "34FTG234+ +- *" ;
let len = s.length;
document.write(groupCharacters(s.split( "" ), len));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...