Open In App

Generate two output strings depending upon occurrence of character in input string.

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an input string str[], generate two output strings. One of which consists of those character which occurs only once in input string and second which consists of multi-time occurring characters. Output strings must be sorted.
Examples: 
 

Input : str[] = "geeksforgeeks"
Output : String with characters occurring once:
for
String with characters occurring multiple times:
egks

Input : str[] = "geekspractice"
Output : String with characters occurring once:
agikprst
String with characters occurring multiple times:
ce


 


Approach : We follow total two steps to generate the both output strings. 
Step 1: Create a count array and count occurrences of characters in the given input string. 
Step 2: Check count array for each position 'i' which leads to three possible conditions : 
a) If count value is 1, append character in first output string. 
b) If count value is greater than 1, append character in second output string. 
c) If count value is 0 do nothing.
Time Complexity for above approach is O(n). 
Auxiliary Space required is O(1). 
 

C++
// CPP program to print two strings
// made of character occurring once 
// and multiple times
#include <bits/stdc++.h>
using namespace std;

const int MAX_CHAR = 256;

// function to print two strings 
// generated from single string one
// with characters occurring once 
// other with character occurring 
// multiple of times
void printDuo(string &str)
{
    // initialize hashtable with zero 
    // entry
    int countChar[MAX_CHAR] = { 0 };

    // perform hashing for input string
    int n = str.length();
    for (int i = 0; i < n; i++)
        countChar[str[i] - 'a']++;

    // generate string (str1) consisting 
    // char occurring once and string 
    // (str2) consisting char occurring 
    // multiple times
    string str1 = "", str2 = "";  
    for (int i = 0; i < MAX_CHAR; i++) {
        if (countChar[i] > 1)
            str2 = str2 + (char)(i + 'a');
        else if (countChar[i] == 1)
            str1 = str1 + (char)(i + 'a');
    }

    // print both strings
    cout << "String with characters occurring "
         <<  "once:\n";
    cout << str1 << "\n";
    cout << "String with characters occurring " 
         << "multiple times:\n";
    cout << str2 << "\n";
}

// driver program
int main()
{
    string str = "lovetocode";
    printDuo(str);
    return 0;
}
Java Python C# Javascript

Output: 
 

String with characters occurring once:
cdltv
String with characters occurring multiple times:
eo

Time complexity : O(n) 
Auxiliary Space : O(1)


Approach : Using Sets

Follwo the below steps:

  • Create a set to store characters occurring only once.
  • Create another set to store characters occurring multiple times.
  • Iterate through the input string and populate the sets accordingly.
  • Convert sets to sorted strings and return them.

Below is the implementation of the above approach :

def separate_characters(input_str):
    # Initialize sets to store characters occurring only once and multiple times
    singles_set = set()
    multiples_set = set()
    
    # Iterate through the input string and populate the sets accordingly
    char_count = {}
    for char in input_str:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1
            
    for char, count in char_count.items():
        if count == 1:
            singles_set.add(char)
        else:
            multiples_set.add(char)
    
    # Convert sets to sorted strings
    singles = ''.join(sorted(singles_set))
    multiples = ''.join(sorted(multiples_set))
    
    return singles, multiples

# Example usage:
input_str1 = "geeksforgeeks"
singles1, multiples1 = separate_characters(input_str1)
print("String with characters occurring once:", singles1)
print("String with characters occurring multiple times:", multiples1)

input_str2 = "geekspractice"
singles2, multiples2 = separate_characters(input_str2)
print("String with characters occurring once:", singles2)
print("String with characters occurring multiple times:", multiples2)

Output
String with characters occurring once: for
String with characters occurring multiple times: egks
String with characters occurring once: agikprst
String with characters occurring multiple times: ce

Time complexity: O(n + k * log(k)), where n is the length of the input string & k is the number of unique characters

Auxiliary space : O(c), where c is the number of unique characters.
 


Next Article
Article Tags :
Practice Tags :

Similar Reads