Open In App

# Find the first repeated word in a string in Python using Dictionary

Prerequisite : Dictionary data structure Given a string, Find the 1st repeated word in a string. Examples:

```Input : "Ravi had been saying that he had been there"

Input : "Ravi had been saying that"
Output : No Repetition

Output : he```

We have existing solution for this problem please refer Find the first repeated word in a string link. We can solve this problem quickly in python using Dictionary data structure. Approach is simple,

1. First split given string separated by space.
2. Now convert list of words into dictionary using collections.Counter(iterator) method. Dictionary contains words as key and it’s frequency as value.
3. Now traverse list of words again and check which first word has frequency greater than 1.

## Python3

 `# Function to Find the first repeated word in a string``from` `collections ``import` `Counter` `def` `firstRepeat(``input``):``    ` `    ``# first split given string separated by space``    ``# into words``    ``words ``=` `input``.split(``' '``)``    ` `    ``# now convert list of words into dictionary``    ``dict` `=` `Counter(words)` `    ``# traverse list of words and check which first word``    ``# has frequency > 1``    ``for` `key ``in` `words:``        ``if` `dict``[key]>``1``:``            ``print` `(key)``            ``return` `# Driver program``if` `__name__ ``=``=` `"__main__":``    ``input` `=` `'Ravi had been saying that he had been there'``    ``firstRepeat(``input``)`

Output:

`had`

Time Complexity: O(length(words))

Auxiliary Space: O(length(dict))

#### Approach

We can iterate through the words in the input string and store their frequency in a dictionary. If we encounter a word that has already been seen before (i.e., its frequency is greater than 1), we return that word as the first repeated word. If we finish iterating through all the words without finding a repeated word, we return “No Repetition”.

#### Algorithm

1. Initialize an empty dictionary “word_freq”.
2. Split the input string into words using the split() function.
3. Iterate through each word in the list of words:
a. If the word is not already in the dictionary, add it with a frequency of 1.
b. If the word is already in the dictionary, increment its frequency.
c. If the frequency of the word is greater than 1, return the word as the first repeated word.
4. If we finish iterating through all the words without finding a repeated word, return “No Repetition”.

## Python3

 `def` `find_first_repeated_word(input_string):``    ``word_freq ``=` `{}``    ``words ``=` `input_string.split()``    ``for` `word ``in` `words:``        ``if` `word ``not` `in` `word_freq:``            ``word_freq[word] ``=` `1``        ``else``:``            ``word_freq[word] ``+``=` `1``        ``if` `word_freq[word] > ``1``:``            ``return` `word``    ``return` `"No Repetition"``input_string``=` `'Ravi had been saying that he had been there'``print``(find_first_repeated_word(input_string))`

Output

`had`

Time Complexity: O(n), where n is the number of words in the input string.

Auxiliary Space: O(n), where n is the number of words in the input string.

#### Approach#3: Using defaultdict

This approach used in the above solution is to use a dictionary to keep track of the frequency of each word in the input string. We iterate through each word in the input string and update the frequency count in the dictionary. If the frequency of any word becomes greater than 1, it means that the word has been repeated and we return that word as the first repeated word.

#### Algorithm

1. Initialize an empty dictionary to store the frequency count of each word.
2. Split the input string into individual words using the split() method.
3. Iterate through each word in the list of words.
4. For each word, update its frequency count in the dictionary.
5. If the frequency of any word becomes greater than 1, set that word as the first repeated word and break out of the loop.
6. If no word is repeated, return “No Repetition”.
7. Otherwise, return the first repeated word.

## Python3

 `from` `collections ``import` `defaultdict` `string ``=` `"Ravi had been saying that he had been there"` `word_counts ``=` `defaultdict(``int``)``first_repeated_word ``=` `None` `for` `word ``in` `string.split():``    ``word_counts[word] ``+``=` `1``    ``if` `word_counts[word] > ``1``:``        ``first_repeated_word ``=` `word``        ``break` `output ``=` `first_repeated_word ``if` `first_repeated_word ``else` `"No Repetition"``print``(output)`

Output

`had`

Time Complexity: O(n), where n is the number of words in the input string. We iterate through the list of words only once and perform constant-time dictionary lookups for each word.

Auxiliary Space: O(n), where n is the number of words in the input string. We need to store the frequency count of each word in a dictionary, which can have up to n key-value pairs in the worst case.