Monday, November 18, 2024
Google search engine
HomeData Modelling & AIGroup consecutive characters of same type in a string

Group consecutive characters of same type in a string

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the modified string
string groupCharacters(string s, int len)
{
 
    // Store original string
    string temp = "";
 
    // Remove all white spaces
    for (int i = 0; i < len; i++)
        if (s[i] != ' ')
            temp = temp + s[i];
 
    len = temp.length();
 
    // To store the resultant string
    string ans = "";
    int i = 0;
 
    // Traverse the string
    while (i < len) {
 
        // Group upper case characters
        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 + " ";
        }
 
        // Group numeric characters
        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 + " ";
        }
 
        // Group arithmetic operators
        else {
            while (i < len && int(temp[i]) >= int('*')
                   && int(temp[i]) <= int('/')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    }
 
    // Return the resultant string
    return ans;
}
 
// Driver code
int main()
{
    string s = "34FTG234+ +- *";
    int len = s.length();
    cout << groupCharacters(s, len);
    return 0;
}


Java




import java.io.*;
// Java implementation of the approach
class GFG {
 
    // Function to return the modified String
    static String groupCharacters(char[] s, int len)
    {
 
        // Store original String
        String temp = "";
 
        // Remove all white spaces
        for (int i = 0; i < len; i++)
            if (s[i] != ' ')
                temp = temp + s[i];
 
        len = temp.length();
 
        // To store the resultant String
        String ans = "";
        int i = 0;
 
        // Traverse the String
        while (i < len) {
 
            // Group upper case characters
            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 + " ";
            }
 
            // Group numeric characters
            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 + " ";
            }
 
            // Group arithmetic operators
            else {
                while (i < len && temp.charAt(i) >= ('*')
                       && temp.charAt(i) <= ('/')) {
                    ans = ans + temp.charAt(i);
                    i++;
                }
                ans = ans + " ";
            }
        }
 
        // Return the resultant String
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "34FTG234+ +- *";
        int len = s.length();
        System.out.print(
            groupCharacters(s.toCharArray(), len));
    }
}
// This code contributed by Rajput-Ji


Python 3




# Python 3 implementation of the approach
 
# Function to return the modified string
 
 
def groupCharacters(s, l):
 
    # Store original string
    temp = ""
 
    # Remove all white spaces
    for i in range(l):
        if (s[i] != ' '):
            temp = temp + s[i]
 
    l = len(temp)
 
    # To store the resultant string
    ans = ""
    i = 0
 
    # Traverse the string
    while (i < l):
 
        # Group upper case characters
        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 + " "
 
        # Group numeric characters
        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 + " "
 
        # Group arithmetic operators
        else:
            while (i < l and ord(temp[i]) >= ord('*')
                   and ord(temp[i]) <= ord('/')):
                ans = ans + temp[i]
                i += 1
            ans = ans + " "
 
    # Return the resultant string
    return ans
 
 
# Driver code
if __name__ == "__main__":
 
    s = "34FTG234+ +- *"
    l = len(s)
    print(groupCharacters(s, l))
 
# This code is contributed by Ita_c


C#




// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the modified String
    static String groupCharacters(char[] s, int len)
    {
 
        // Store original String
        string temp = "";
 
        // Remove all white spaces
        for (int j = 0; j < len; j++)
            if (s[j] != ' ')
                temp = temp + s[j];
 
        len = temp.Length;
 
        // To store the resultant String
        string ans = "";
        int i = 0;
 
        // Traverse the String
        while (i < len) {
 
            // Group upper case characters
            if (temp[i] >= ('A') && temp[i] <= ('Z')) {
                while (i < len && temp[i] >= ('A')
                       && temp[i] <= ('Z')) {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
 
            // Group numeric characters
            else if (temp[i] >= ('0') && temp[i] <= ('9')) {
                while (i < len && temp[i] >= ('0')
                       && temp[i] <= ('9')) {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
 
            // Group arithmetic operators
            else {
                while (i < len && temp[i] >= ('*')
                       && temp[i] <= ('/')) {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
        }
 
        // Return the resultant String
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        string s = "34FTG234+ +- *";
        int len = s.Length;
        Console.WriteLine(
            groupCharacters(s.ToCharArray(), len));
    }
}
 
// This code contributed by Ryuga


PHP




<?php
// PHP implementation of the approach
 
// Function to return the modified string
function groupCharacters($s, $len)
{
 
    // Store original string
    $temp = "";
 
    // Remove all white spaces
    for ($i = 0; $i < $len; $i++)
        if ($s[$i] != ' ')
            $temp = $temp.$s[$i];
 
    $len = strlen($temp);
 
    // To store the resultant string
    $ans = "";
    $i = 0;
 
    // Traverse the string
    while ($i < $len)
    {
 
        // Group upper case characters
        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 . " ";
        }
 
        // Group numeric characters
        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 . " ";
        }
 
        // Group arithmetic operators
        else
        {
            while ($i < $len && ord($temp[$i]) >= ord('*') &&
                                ord($temp[$i]) <= ord('/'))
            {
                $ans = $ans.$temp[$i];
                $i++;
            }
            $ans = $ans . " ";
        }
    }
 
    // Return the resultant string
    return $ans;
}
 
// Driver code
$s = "34FTG234+ +- *";
$len = strlen($s);
print(groupCharacters($s, $len));
     
// This code is contributed by mits
?>


Javascript




<script>
// Javascript implementation of the approach
 
// Function to return the modified String
function groupCharacters(s,len)
{
    // Store original String
    let temp = "";
    
    // Remove all white spaces
    for (let i = 0; i < len; i++)
        if (s[i] != ' ')
            temp = temp + s[i];
    
    len = temp.length;
    
    // To store the resultant String
    let ans = "";
    let i = 0;
    
    // Traverse the String
    while (i < len) {
    
        // Group upper case characters
        if (temp[i] >= ('A')
            && temp[i] <= ('Z')) {
            while (i < len && temp[i] >= ('A')
                   && temp[i] <= ('Z')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    
        // Group numeric characters
        else if (temp[i] >= ('0')
                 && temp[i] <= ('9')) {
            while (i < len && temp[i] >= ('0')
                   && temp[i] <= ('9')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    
        // Group arithmetic operators
        else {
            while (i < len && temp[i] >= ('*')
                   && temp[i] <= ('/')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    }
    
    // Return the resultant String
    return ans;
}
 
// Driver code
let s = "34FTG234+ +- *";
let len = s.length;
document.write(groupCharacters(s.split(""), len));
 
 
         
// This code is contributed by rag2127
</script>


Output: 

34 FTG 234 ++-*

 

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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments