给定两个字符串A和B ,任务是打印出两个给定句子中的所有非重复单词。
例子:
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
方法:这个想法是迭代所有单词并检查单词是否被重复。因此,可以按照以下步骤来计算答案:
- 连接两个字符串并将其存储在另一个字符串变量中。
- 从连接的字符串提取一个单词。
- 如果该词出现在 A 或 B 中,则打印它。否则,继续剩下的单词。
下面是上述方法的实现:
C++
// C++ program to print all the
// non-repeating words from the
// two given sentences
#include
#include
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
输出:
have a pen I want to park
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live