原文:

给定两个字符串 ab ,任务是打印两个给定句子中所有不重复的单词。 例:

输入: a =“我有蓝笔”,b =“我有红笔” 输出:有蓝有红 解释: have、blue、got 和 red 这几个词在同一个句子或另一个句子中都没有重复。 输入: a =“我要停车”,b =“我在停车” 输出:要进去

方法:想法是,检查单词是否重复。因此,可以按照以下步骤计算答案:

  1. 并将其存储在另一个字符串变量中。
  2. 如果该单词出现在 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

python 3

# 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

java 描述语言


output: 

have a pen i want to park