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++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
void printDuo(string &str;)
{
int countChar[MAX_CHAR] = { 0 };
int n = str.length();
for (int i = 0; i < n; i++)
countChar[str[i] - 'a']++;
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');
}
cout << "String with characters occurring "
<< "once:\n";
cout << str1 << "\n";
cout << "String with characters occurring "
<< "multiple times:\n";
cout << str2 << "\n";
}
int main()
{
string str = "lovetocode";
printDuo(str);
return 0;
}
Java
class GFG {
final static int MAX_CHAR = 256;
static void printDuo(String str) {
int countChar[] = new int[MAX_CHAR];
int n = str.length();
for (int i = 0; i < n; i++) {
countChar[str.charAt(i) - 'a']++;
}
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');
}
}
System.out.print("String with characters occurring "
+ "once:\n");
System.out.print(str1 + "\n");
System.out.print("String with characters occurring "
+ "multiple times:\n");
System.out.print(str2 + "\n");
System.out.print("");
}
public static void main(String[] args) {
String str = "lovetocode";
printDuo(str);
}
}
Python3
MAX_CHAR = 256
def printDuo(string):
countChar = [0 for i in range(MAX_CHAR)]
n = len(string)
for i in range(n):
countChar[ord(string[i]) - ord('a')] += 1
str1 = ""
str2 = ""
for i in range(MAX_CHAR):
if (countChar[i] > 1):
str2 = str2 + chr(i + ord('a'))
elif (countChar[i] == 1):
str1 = str1 + chr(i + ord('a'))
print("String with characters occurring once:",
"\n", str1)
print("String with characters occurring",
"multiple times:", "\n", str2)
string = "lovetocode"
printDuo(string)
C#
using System;
class GFG
{
static int MAX_CHAR = 256;
static void printDuo(string str)
{
int[] countChar = new int[MAX_CHAR];
int n = str.Length;
for (int i = 0; i < n; i++)
{
countChar[str[i] - 'a']++;
}
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');
}
}
Console.Write("String with characters "+
"occurring once:\n");
Console.Write(str1 + "\n");
Console.Write("String with characters occurring " +
"multiple times:\n");
Console.Write(str2 + "\n");
Console.Write("");
}
public static void Main()
{
string str = "lovetocode";
printDuo(str);
}
}
Javascript
<script>
var MAX_CHAR = 256;
function printDuo(str)
{
var countChar = Array(MAX_CHAR).fill(0);
var n = str.length;
for (var i = 0; i < n; i++)
countChar[str[i].charCodeAt(0) - 'a'.charCodeAt(0)]++;
var str1 = "", str2 = "";
for (var i = 0; i < MAX_CHAR; i++) {
if (countChar[i] > 1)
str2 = str2 + String.fromCharCode(i + 'a'.charCodeAt(0));
else if (countChar[i] == 1)
str1 = str1 + String.fromCharCode(i + 'a'.charCodeAt(0));
}
document.write( "String with characters occurring "
+ "once:<br>");
document.write( str1 + "<br>");
document.write( "String with characters occurring "
+ "multiple times:<br>");
document.write( str2 + "<br>");
}
var str = "lovetocode";
printDuo(str);
</script>
Output:
String with characters occurring once:
cdltv
String with characters occurring multiple times:
eo
Time complexity : O(n) Auxiliary Space : O(1)
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...