Dada una string de entrada str[], genera dos strings de salida. Uno de los cuales consiste en aquellos caracteres que ocurren solo una vez en la string de entrada y el segundo que consiste en caracteres que ocurren varias veces. Las strings de salida deben estar ordenadas.
Ejemplos:
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
Enfoque: Seguimos un total de dos pasos para generar ambas strings de salida.
Paso 1: Cree una array de conteo y cuente las ocurrencias de caracteres en la string de entrada dada.
Paso 2: verifique la array de conteo para cada posición ‘i’, lo que conduce a tres condiciones posibles:
a) Si el valor de conteo es 1, agregue el carácter en la primera string de salida.
b) Si el valor de conteo es mayor que 1, agregue el carácter en la segunda string de salida.
c) Si el valor de conteo es 0, no haga nada.
La complejidad del tiempo para el enfoque anterior es O(n).
El espacio auxiliar requerido es 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
// Java program to print two strings // made of character occurring once // and multiple times class GFG { final static 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 static void printDuo(String str) { // initialize hashtable with zero // entry int countChar[] = new int[MAX_CHAR]; // perform hashing for input string int n = str.length(); for (int i = 0; i < n; i++) { countChar[str.charAt(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 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(""); } // driver program public static void main(String[] args) { String str = "lovetocode"; printDuo(str); } } //this code contributed by 29AJayKumar
Python3
# Python3 program to print two strings # made of character occurring once # and multiple times MAX_CHAR = 256 # function to print two strings # generated from single string one # with characters occurring once # other with character occurring # multiple of times def printDuo(string): # initialize hashtable with zero # entry countChar = [0 for i in range(MAX_CHAR)] # perform hashing for input string n = len(string) for i in range(n): countChar[ord(string[i]) - ord('a')] += 1 # generate string (str1) consisting # char occurring once and string # (str2) consisting char occurring # multiple times 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 both strings print("String with characters occurring once:", "\n", str1) print("String with characters occurring", "multiple times:", "\n", str2) # Driver Code string = "lovetocode" printDuo(string) # This code is contributed by # Mohit kumar 29
C#
// C# program to print two strings // made of character occurring once // and multiple times using System; class GFG { static 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 static void printDuo(string str) { // initialize hashtable with zero // entry int[] countChar = new int[MAX_CHAR]; // 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 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(""); } // Driver Code public static void Main() { string str = "lovetocode"; printDuo(str); } } // This code is contributed by ita_c
Javascript
<script> // javascript program to print two strings // made of character occurring once // and multiple times var MAX_CHAR = 256; // function to print two strings // generated from single string one // with characters occurring once // other with character occurring // multiple of times function printDuo(str) { // initialize hashtable with zero // entry var countChar = Array(MAX_CHAR).fill(0); // perform hashing for input string var n = str.length; for (var i = 0; i < n; i++) countChar[str[i].charCodeAt(0) - 'a'.charCodeAt(0)]++; // generate string (str1) consisting // char occurring once and string // (str2) consisting char occurring // multiple times 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)); } // print both strings 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>"); } // driver program var str = "lovetocode"; printDuo(str); </script>
Producción:
String with characters occurring once: cdltv String with characters occurring multiple times: eo
Complejidad temporal : O(n)
Espacio auxiliar : O(1)
Este artículo es una contribución de Shivam Pradhan (anuj_charm) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA