C++Server Side ProgrammingProgramming In this problem, we are a string str consisting of comma separated words. Our task is to find the first repeated word in a string. We need to find the first word 'string between two spaces' which gets repeated in the string. Let's take an example to understand the problem, Input : str = "C program are easy to program" Output : programSolution ApproachA simple solution to the problem is using hashmap data structure. To find the first repeated word, we will store each word and its count (number of times it appeared in the string ) in the hashmap. For this we will keep checking if the current word is present or not. Then we will print the first work with more than one occurrence count in the hashmap. ExampleProgram to illustrate the working of our solution #include <bits/stdc++.h> using namespace std; string findFirstRepeatWord(string str){ istringstream iss(str); string word; unordered_map<string, int> wordCountMap; while (getline(iss, word, ' ')) { if (wordCountMap.find(word) != wordCountMap.end()) wordCountMap[word] ++; else wordCountMap.insert(make_pair(word, 1)); } istringstream iss2(str); while (getline(iss2, word, ' ')) { int count = wordCountMap[word]; if (count > 1) { return word; } } return "NoRepetition"; } int main(){ string str = "C program are easy to program"; string repeatedWord = findFirstRepeatWord(str); if (repeatedWord != "NoRepetition") cout<<"The first repeated word is '"<<repeatedWord<<"'"; else cout<<"No word is Repeated in the string"; return 0; }OutputThe first repeated word is 'program'This program uses a lot of in-build functions for making the task easier.
Updated on 27-Jan-2022 10:50:35
Map and Map.Entry interface will be used as the Map interface maps unique keys to values. A key is an object that is used to retrieve a value at a later date. The Map.Entry interface enables you to work with a map entry. Also, we will use the HashMap class to store items in “key/value” pairs and access them by an index of another type. Illustration:
Implementation: Sample file input image is as follows: Example
Output:
Given a string, Find the 1st repeated word in a string Examples: Input : "Ravi had been saying that he had been there" Output : had Input : "Ravi had been saying that" Output : No Repetition Input : "he had had he" Output : hequestion source : https://www.geeksforgeeks.org/goldman-sachs-interview-experience-set-29-internship/amp/ Simple Approach : Start iterating from back and for every new word , store it in unordered map . For every word which has occurred more than one , update ans to be that word , at last reverse ans and print it. Implementation:
Output had No Repetition he Another Approach: The idea is to tokenize the string and store each word and its count in hashmap. Then traverse the string again and for each word of string, check its count in created hashmap. Implementation:
Output First repeated word :: had
Implementation::
Instead of tracking the counts for a specific token(word), we can keep track of the first occurrence of the token(word) using an unordered map. This would not require any extra loop to traverse in a hashmap or a string to find the repeated string. Thus, it eventually transforms the time complexity from O(2*n) to O(n) while the space complexity remains the same. Implementation:
Output had No Repetition he Instead of counting a number of occurrences of each word which will have O(N) time and space complexity, where N is number of words, we can stop when the count of any word becomes 2. That is no need to iterate through all the words in string. Let’s say our first repeated word is present at Mth index, then By using this approach, space and time complexity reduced from O(N) to O(M). Where, N: number of words in a string. M: Index at which first repeating word is present However, Worst case( When no word is being repeated or the word being repeated is present at last) time and space complexity will still be O(N). Steps:
Implementation:
Time complexity: O(M) Optimized Approach 2:Instead of counting a number of occurrences of each word which will have O(N) time and space complexity, where N is a number of words, we can just store words in a HashSet, and as soon as we reach a word that is already present in the HashSet we can return. Implementation:
Output First repeated word :: had This article is contributed by Aarti_Rathi and Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Article Tags : |