📌  相关文章
📜  通过替换字符以相同的或剩余的字符串重复修改字符串数组

📅  最后修改于: 2021-04-17 16:45:35             🧑  作者: Mango

给定字符串数组的常用3 []由小写和大写字符只,任务是由来自在相同的字符串或任何其他字符串的重复字符串除去字符来修改阵列。打印修改后的数组。

例子:

方法:按照以下步骤解决问题:

  • 初始化一个无序集合,以在遍历数组时存储字符串的字符。
  • 遍历数组,并对每个字符串执行以下操作:
    • 遍历字符串的字符。
    • 如果当前字符已存在于Set中,请跳过它。否则,将其附加到输出字符串。
    • 将新生成的字符串推入已初始化以存储输出的列表中。
  • 打印获得的字符串列表作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to remove duplicate
// characters across the strings
void removeDuplicateCharacters(vector arr)
{
    // Stores distinct characters
    unordered_set cset;
 
    // Size of the array
    int n = arr.size();
 
    // Stores the list of
    // modified strings
    vector out;
 
    // Traverse the array
    for (auto str : arr) {
 
        // Stores the modiifed string
        string out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for (auto ch : str) {
 
            // If charcter is already present
            if (cset.find(ch) != cset.end())
                continue;
 
            out_curr += ch;
 
            // Insert character into the Set
            cset.insert(ch);
        }
 
        if (out_curr.size())
            out.push_back(out_curr);
    }
 
    // Print the list of modified strings
    for (int i = 0; i < out.size(); i++) {
 
        // Print each string
        cout << out[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array of strings
    vector arr
        = { "Geeks", "For", "Geeks", "Post" };
 
    // Function Call to modify the
    // given array of strings
    removeDuplicateCharacters(arr);
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to remove duplicate
// characters across the strings
static void removeDuplicateCharacters(String arr[])
{
     
    // Stores distinct characters
    HashSet cset = new HashSet<>();
 
    // Size of the array
    int n = arr.length;
 
    // Stores the list of
    // modified strings
    ArrayList out = new ArrayList<>();
 
    // Traverse the array
    for(String str : arr)
    {
         
        // Stores the modiifed string
        String out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for(char ch : str.toCharArray())
        {
             
            // If charcter is already present
            if (cset.contains(ch))
                continue;
 
            out_curr += ch;
 
            // Insert character into the Set
            cset.add(ch);
        }
 
        if (out_curr.length() != 0)
            out.add(out_curr);
    }
 
    // Print the list of modified strings
    for(int i = 0; i < out.size(); i++)
    {
         
        // Print each string
        System.out.print(out.get(i) + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given array of strings
    String arr[] = { "Geeks", "For", "Geeks", "Post" };
 
    // Function Call to modify the
    // given array of strings
    removeDuplicateCharacters(arr);
}
}
 
// This code is contributed by Kingash


Python3
# Python 3 program for the above approach
 
# Function to remove duplicate
# characters across the strings
def removeDuplicateCharacters(arr):
 
    # Stores distinct characters
    cset = set([])
 
    # Size of the array
    n = len(arr)
 
    # Stores the list of
    # modified strings
    out = []
 
    # Traverse the array
    for st in arr:
 
        # Stores the modiifed string
        out_curr = ""
 
        # Iterate over the characters
        # of the modified string
        for ch in st:
 
            # If charcter is already present
            if (ch in cset):
                continue
 
            out_curr += ch
 
            # Insert character into the Set
            cset.add(ch)
 
        if (len(out_curr)):
            out.append(out_curr)
 
    # Print the list of modified strings
    for i in range(len(out)):
 
        # Print each string
        print(out[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given array of strings
    arr = ["Geeks", "For", "Geeks", "Post"]
 
    # Function Call to modify the
    # given array of strings
    removeDuplicateCharacters(arr)
 
    # This code is contributed by ukasp.


输出:
Geks For Pt

时间复杂度: O(N * M),其中M是数组中最长字符串的长度。
辅助空间: O(N)