Print all the non-repeating words from the two given sentences
Last Updated :
20 Dec, 2022
Given two strings A and B, the task is to print all the non-repeating words out of the two given sentences.
Examples:
Input: A = “I have a blue pen”, B = “I got a red pen”
Output: have blue got red
Explanation:
The words have, blue, got and red have not been repeated in either the same sentence or another sentence.
Input: A = “I am going to park”, B = “I am in park”
Output: going to in
Approach: The idea is to iterate over all the words and check if the word is being repeated or not. Therefore, the following steps can be followed to compute the answer:
- Concatenate both the string and store it in another string variable.
- Extract one word from the concatenated string.
- If that word is present in either A or in B, then print it. Else, continue for the remaining words.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
void removeRepeating(string s1, string s2)
{
string s3 = s1 + " " + s2 + " " ;
string words = "" ;
int i = 0;
for ( auto x : s3) {
if (x == ' ' ) {
if (s1.find(words) == string::npos
|| s2.find(words) == string::npos)
cout << words;
words = "" ;
}
else {
words = words + x;
}
}
}
int main()
{
string s1 = "I have go a pen" ;
string s2 = "I want to go park" ;
removeRepeating(s1, s2);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void removeRepeating(String s1, String s2)
{
String s3 = s1 + " " + s2 + " " ;
String words = "" ;
int i = 0 ;
for ( char x : s3.toCharArray())
{
if (x == ' ' )
{
if (!s1.contains(words) ||
!s2.contains(words))
System.out.print(words);
words = " " ;
}
else
{
words = words + x;
}
}
}
public static void main(String[] args)
{
String s1 = "I have go a pen" ;
String s2 = "I want to go park" ;
removeRepeating(s1, s2);
}
}
|
Python3
def removeRepeating(s1, s2):
s3 = s1 + " " + s2 + " "
words = ""
i = 0
for x in s3:
if (x = = ' ' ):
if (words not in s1 or
words not in s2):
print (words, end = "")
words = " "
else :
words = words + x
if __name__ = = "__main__" :
s1 = "I have go a pen"
s2 = "I want to go park"
removeRepeating(s1, s2)
|
C#
using System;
class GFG{
static void removeRepeating( string s1,
string s2)
{
string s3 = s1 + " " + s2 + " " ;
string words = "" ;
int i = 0;
foreach ( char x in s3.ToCharArray())
{
if (x == ' ' )
{
if (!s1.Contains(words) ||
!s2.Contains(words))
Console.Write(words);
words = " " ;
}
else
{
words = words + x;
}
}
}
public static void Main( string [] args)
{
string s1 = "I have go a pen" ;
string s2 = "I want to go park" ;
removeRepeating(s1, s2);
}
}
|
Javascript
<script>
function removeRepeating(s1,s2)
{
let s3 = s1 + " " + s2 + " " ;
let words = "" ;
let i = 0;
let temp = s3.split( "" )
for (let x = 0; x < temp.length; x++)
{
if (temp[x] == ' ' )
{
if (!s1.includes(words) ||
!s2.includes(words))
document.write(words);
words = " " ;
}
else
{
words = words + temp[x];
}
}
}
let s1 = "I have go a pen" ;
let s2 = "I want to go park" ;
removeRepeating(s1, s2);
</script>
|
Output:
have a pen I want to park
Time complexity: O(M+N) where M and N is length of string s1 and string s2
Auxiliary space: O(M+N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...