📌  相关文章
📜  根据输入字符串中出现的字符生成两个输出字符串。

📅  最后修改于: 2022-05-13 01:57:08.845000             🧑  作者: Mango

根据输入字符串中出现的字符生成两个输出字符串。

给定一个输入字符串str[],生成两个输出字符串。其中一个由在输入字符串中仅出现一次的字符组成,第二个由多次出现的字符组成。必须对输出字符串进行排序。
例子:

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

方法:我们按照总共两个步骤来生成两个输出字符串。
第 1 步:创建一个计数数组并计算给定输入字符串中字符的出现次数。
第 2 步:检查每个位置“i”的计数数组,这会导致三种可能的情况:
a) 如果计数值为 1,则在第一个输出字符串中追加字符。
b) 如果计数值大于 1,则在第二个输出字符串中追加字符。
c) 如果计数值为 0,则什么也不做。
上述方法的时间复杂度为 O(n)。
所需的辅助空间是 O(1)。

C++
// CPP program to print two strings
// made of character occurring once
// and multiple times
#include 
using namespace std;
 
const int MAX_CHAR = 256;
 
// function to print two strings
// generated from single string one
// with characters occurring onces
// 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 onces
// 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 onces
# 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 onces
// 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


输出:

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