Modifique la array de strings reemplazando los caracteres que se repiten en las mismas strings o en las strings restantes

Dada una array de strings arr[] que consta solo de caracteres en minúsculas y mayúsculas, la tarea es modificar la array eliminando los caracteres de las strings que se repiten en la misma string o en cualquier otra string. Imprime la array modificada. 

Ejemplos:

Entrada: arr[] = {“Geeks”, “For”, “Geeks”}
Salida:  {“Geks”, “For”}
Explicación:
En arr[0[, ‘e’ aparece dos veces en la string. Quitar una sola ‘e’ de la primera string modifica «Geeks» a «Geks». 
En arr[1], todos los caracteres no se repiten. Por lo tanto, la string permanece sin cambios.
En arr[2], la string es la misma que arr[0]. Por lo tanto, se requiere eliminar la string completa.

Entrada: arr[] = {“Geeks”, “Para”, “Geeks”, “Publicar”}
Salida: {“Geks”, “Para”, “Pt”}

Enfoque: Siga los pasos para resolver el problema:

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove duplicate
// characters across the strings
void removeDuplicateCharacters(vector<string> arr)
{
    // Stores distinct characters
    unordered_set<char> cset;
 
    // Size of the array
    int n = arr.size();
 
    // Stores the list of
    // modified strings
    vector<string> out;
 
    // Traverse the array
    for (auto str : arr) {
 
        // Stores the modified string
        string out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for (auto ch : str) {
 
            // If character 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<string> 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<Character> cset = new HashSet<>();
 
    // Size of the array
    int n = arr.length;
 
    // Stores the list of
    // modified strings
    ArrayList<String> out = new ArrayList<>();
 
    // Traverse the array
    for(String str : arr)
    {
         
        // Stores the modified string
        String out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        for(char ch : str.toCharArray())
        {
             
            // If character 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 modified string
        out_curr = ""
 
        # Iterate over the characters
        # of the modified string
        for ch in st:
 
            # If character 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.

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to remove duplicate
// characters across the strings
static void removeDuplicateCharacters(string[] arr)
{
     
    // Stores distinct characters
    HashSet<int> cset = new HashSet<int>();
  
    // Size of the array
    int n = arr.Length;
  
    // Stores the list of
    // modified strings
    List<string> Out = new List<string>();
  
    // Traverse the array
    foreach(string str in arr)
    {
         
        // Stores the modified string
        string out_curr = "";
  
        // Iterate over the characters
        // of the modified string
        foreach(char ch in str.ToCharArray())
        {
              
            // If character 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.Count; i++)
    {
          
        // Print each string
        Console.Write(Out[i] + " ");
    }
}
     
    static public void Main (){
         
        // 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 avanitrachhadiya2155

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to remove duplicate
// characters across the strings
function removeDuplicateCharacters(arr)
{
    // Stores distinct characters
    var cset = new Set();
 
    // Size of the array
    var n = arr.length;
 
    // Stores the list of
    // modified strings
    var out = [];
 
    // Traverse the array
    arr.forEach(str => {
         
 
        // Stores the modified string
        var out_curr = "";
 
        // Iterate over the characters
        // of the modified string
        str.split('').forEach(ch => {
 
            // If character is already present
            if (!cset.has(ch))
            {
 
            out_curr += ch;
 
            // Insert character into the Set
            cset.add(ch);
            }
        });
 
        if (out_curr.size!=0)
            out.push(out_curr);
    });
 
    // Print the list of modified strings
    for (var i = 0; i < out.length; i++) {
 
        // Print each string
        document.write( out[i] + " ");
    }
}
 
// Driver Code
// Given array of strings
var arr
    = ["Geeks", "For", "Geeks", "Post"];
// Function Call to modify the
// given array of strings
removeDuplicateCharacters(arr);
 
</script>
Producción: 

Geks For Pt

 

Complejidad de tiempo: O(N * M) donde M es la longitud de la string más larga de la array.
Espacio Auxiliar: O(N)

Publicación traducida automáticamente

Artículo escrito por DevanshuAgarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *