Given two strings A and B, the task is to print all the non-repeating words out of the two given sentences.
Input: A = “I have a blue pen”, B = “I got a red pen”
Output: have blue got red
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 remaining words.
Below is the implementation of the above approach:
have a pen I want to park
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of words that are present in all the given sentences
- Recursively print all sentences that can be formed from list of word lists
- Program to print the given digit in words
- Given a sequence of words, print all anagrams together using STL
- Print all possible words from phone digits
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Print all funny words in a string
- Print all valid words that are possible using Characters of Array
- Print words of a string in reverse order
- Print all possible combinations of words from Dictionary using Trie
- Java program to print Even length words in a String
- Java ArrayList to print all possible words from phone digits
- Print number of words, vowels and frequency of each character
- Extract and print words separately from a given Camel Case string
- Print all words matching a pattern in CamelCase Notation Dictonary
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- Acronym words
- Print shortest path to print a string on screen
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sapnasingh4991