Dadas dos strings A y B , la tarea es imprimir todas las palabras que no se repiten de las dos oraciones dadas.
Ejemplos:
Entrada: A = “Tengo un bolígrafo azul”, B = “Tengo un bolígrafo rojo”
Salida: tengo azul tengo rojo
Explicación:
Las palabras tienen, azul, tengo y rojo no se han repetido ni en la misma oración ni en otra oración .
Entrada: A = “Voy a estacionar”, B = “Estoy en el estacionamiento”
Salida: yendo a en
Enfoque: La idea es iterar sobre todas las palabras y verificar si la palabra se repite o no. Por lo tanto, se pueden seguir los siguientes pasos para calcular la respuesta:
- Concatene la string y guárdela en otra variable de string.
- Extraiga una palabra de la string concatenada.
- Si esa palabra está presente en A o en B, imprímela. De lo contrario, continúe con las palabras restantes.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print all the // non-repeating words from the // two given sentences #include <bits/stdc++.h> #include <string.h> using namespace std; // Function to print all the // non-repeating words from the // two given sentences void removeRepeating(string s1, string s2) { // Concatenate the two strings // into one string s3 = s1 + " " + s2 + " "; string words = ""; int i = 0; // Iterating over the whole // concatenated string for (auto x : s3) { if (x == ' ') { // Searching for the word in A. // If while searching, we reach // the end of the string, then // the word is not present in // the string if (s1.find(words) == string::npos || s2.find(words) == string::npos) cout << words; // Initialise word for the // next iteration words = ""; } else { words = words + x; } } } // Driver code int main() { string s1 = "I have go a pen"; string s2 = "I want to go park"; removeRepeating(s1, s2); return 0; }
Java
// Java program to print all the // non-repeating words from the // two given sentences class GFG{ // Function to print all the // non-repeating words from the // two given sentences static void removeRepeating(String s1, String s2) { // Concatenate the two Strings // into one String s3 = s1 + " " + s2 + " "; String words = ""; int i = 0; // Iterating over the whole // concatenated String for(char x : s3.toCharArray()) { if (x == ' ') { // Searching for the word in A. // If while searching, we reach // the end of the String, then // the word is not present in // the String if (!s1.contains(words) || !s2.contains(words)) System.out.print(words); // Initialise word for the // next iteration words = " "; } else { words = words + x; } } } // Driver code public static void main(String[] args) { String s1 = "I have go a pen"; String s2 = "I want to go park"; removeRepeating(s1, s2); } } // This code is contributed by sapnasingh4991
Python3
# Python 3 program to print all the # non-repeating words from the # two given sentences # Function to print all the # non-repeating words from the # two given sentences def removeRepeating(s1, s2): # Concatenate the two # strings into one s3 = s1 + " " + s2 + " " words = "" i = 0 # Iterating over the whole # concatenated string for x in s3: if (x == ' '): # Searching for the word in A. # If while searching, we reach # the end of the string, then # the word is not present in # the string if (words not in s1 or words not in s2): print(words, end = "") # Initialise word for the # next iteration words = " " else: words = words + x # Driver code if __name__ == "__main__": s1 = "I have go a pen" s2 = "I want to go park" removeRepeating(s1, s2) # This code is contributed by Chitranayal
C#
// C# program to print all the // non-repeating words from the // two given sentences using System; class GFG{ // Function to print all the // non-repeating words from the // two given sentences static void removeRepeating(string s1, string s2) { // Concatenate the two Strings // into one string s3 = s1 + " " + s2 + " "; string words = ""; int i = 0; // Iterating over the whole // concatenated String foreach(char x in s3.ToCharArray()) { if (x == ' ') { // Searching for the word in A. // If while searching, we reach // the end of the String, then // the word is not present in // the String if (!s1.Contains(words) || !s2.Contains(words)) Console.Write(words); // Initialise word for the // next iteration words = " "; } else { words = words + x; } } } // Driver code public static void Main(string[] args) { string s1 = "I have go a pen"; string s2 = "I want to go park"; removeRepeating(s1, s2); } } // This code is contributed by rutvik_56
Javascript
<script> // Javascript program to print all the // non-repeating words from the // two given sentences // Function to print all the // non-repeating words from the // two given sentences function removeRepeating(s1,s2) { // Concatenate the two Strings // into one let s3 = s1 + " " + s2 + " "; let words = ""; let i = 0; // Iterating over the whole // concatenated String let temp = s3.split("") for(let x = 0; x < temp.length; x++) { if (temp[x] == ' ') { // Searching for the word in A. // If while searching, we reach // the end of the String, then // the word is not present in // the String if (!s1.includes(words) || !s2.includes(words)) document.write(words); // Initialise word for the // next iteration words = " "; } else { words = words + temp[x]; } } } // Driver code let s1 = "I have go a pen"; let s2 = "I want to go park"; removeRepeating(s1, s2); // This code is contributed by avanitrachhadiya2155 </script>
Producción:
have a pen I want to park