Open In App

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

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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"
Output : had
 
Input : "Ravi had been saying that"
Output : No Repetition

Input : "he had had he"
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))

Method#2:Using a Dictionary to Store Frequencies of Words

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.



Similar Reads

Find the first repeated word in a string
Given a string, Find the 1st repeated word in a string. Examples: Input: "Ravi had been saying that he had been there"Output: hadInput: "Ravi had been saying that"Output: No Repetition Input: "he had had he" he question source: https://www.geeksforgeeks.org/goldman-sachs-interview-experience-set-29-internship/ Recommended: Please solve it on “PRACT
15+ min read
Efficiently find first repeated character in a string without using any additional data structure in one traversal
Implement a space efficient algorithm to check First repeated character in a string without using any additional data structure in one traversal. Use additional data structures like count array, hash, etc is not allowed. Examples : Input : abcfdeacf Output : char = a, index= 6Recommended: Please solve it on “PRACTICE ” first, before moving on to th
5 min read
Python Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”
4 min read
Find the most repeated word in a text file
Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files, and binary files (written in binary language,0s and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n
2 min read
Find the first repeated character in a string
Given a string, find the first repeated character in it. We need to find the character that occurs more than once and whose index of second occurrence is smallest. A variation of this question is discussed here. Examples: Input: ch = "geeksforgeeks" Output: e e is the first element that repeats Input: str = "hello geeks" Output: l l is the first el
12 min read
Find repeated character present first in a string
Given a string, find the repeated character present first in the string.(Not the first repeated character, found here.) Examples: Input : geeksforgeeks Output : g (mind that it will be g, not e.) Asked in: Goldman Sachs internship Simple Solution using O(N^2) complexity: The solution is to loop through the string for each character and search for t
15 min read
Second most repeated word in a sequence in Python
Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence. (Considering no two words are the second most repeated, there will be always a single word). Examples: Input : {"aaa", "bbb", "ccc", "bbb", "aaa", "aaa"} Output : bbb Input : {"geeks", "for", "geeks", "for", "geeks", "aaa"} Outp
4 min read
Java Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”
5 min read
Javascript Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks
3 min read
C++ Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks
3 min read