📌  相关文章
📜  查找字符串中第一个重复的单词

📅  最后修改于: 2022-05-13 01:57:08.337000             🧑  作者: Mango

查找字符串中第一个重复的单词

给定一个字符串,找出字符串中第一个重复的单词
例子:

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

问题来源:https://www.geeksforgeeks.org/goldman-sachs-interview-experience-set-29-internship/

简单方法:从后面开始迭代,对于每个新单词,将其存储在无序映射中。对于每个出现过一个以上的单词,将 ans 更新为那个单词,最后反转 ans 并打印它。

C++
// Cpp program to find first repeated word in a string
#include
using namespace std;
void solve(string s)
{
    unordered_map mp;  // to store occurrences of word
    string t="",ans="";
    // traversing from back makes sure that we get the word which repeats first as ans
    for(int i=s.length()-1;i>=0;i--)
    {
        // if char present , then add that in temp word string t
        if(s[i]!=' ')
        {
            t+=s[i];
             
        }
        // if space is there then this word t needs to stored in map
        else
        {
            mp[t]++;
            // if that string t has occurred previously then it is a possible ans
            if(mp[t]>1)
               ans=t;
            // set t as empty for again new word  
            t="";
             
        }
    }
     
    // first word like "he" needs to be mapped
            mp[t]++;
            if(mp[t]>1)
               ans=t;
                           
    if(ans!="")
    {
        // reverse ans string as it has characters in reverse order
        reverse(ans.begin(),ans.end());
        cout<


Javascript


CPP
// CPP program for finding first repeated
// word in a string
#include 
using namespace std;
 
// returns first repeated word
string findFirstRepeated(string s)
{
    // break string into tokens
    // and then each string into set
    // if a word appeared before appears
    // again, return the word and break
 
    istringstream iss(s);
    string token;
 
    // hashmap for storing word and its count
    // in sentence
    unordered_map setOfWords;
 
    // store all the words of string
    // and the count of word in hashmap
 
    while (getline(iss, token, ' ')) {
        if (setOfWords.find(token) != setOfWords.end())            
            setOfWords[token] += 1;  // word exists
        else
            // insert new word to set
            setOfWords.insert(make_pair(token, 1));       
    }
 
    // traverse again from first word of string s
    // to check if count of word is greater than 1
 
    // either take a new stream or store the words
    // in vector of strings in previous loop
    istringstream iss2(s);
    while (getline(iss2, token, ' ')) {
        int count = setOfWords[token];
        if (count > 1) {
            return token;
        }
    }
 
    return "NoRepetition";
}
 
// driver program
int main()
{
    string s("Ravi had been saying that he had been there");
    string firstWord = findFirstRepeated(s);
    if (firstWord != "NoRepetition")
        cout << "First repeated word :: "
             << firstWord << endl;
    else
        cout << "No Repetitionn";
    return 0;
}


Java
// Java program for finding first repeated
// word in a string
import java.util.*;
 
class GFG{
     
    // returns first repeated word
    static String findFirstRepeated(String s)
    {
        // break string into tokens
        // and then each string into set
        // if a word appeared before appears
        // again, return the word and break
     
        String token[] = s.split(" ");
     
        // hashmap for storing word and its count
        // in sentence
        HashMap setOfWords = new HashMap();
     
        // store all the words of string
        // and the count of word in hashmap
     
        for (int i=0; i 1) {
                return token[i];
            }
        }
     
        return "NoRepetition";
    }
     
    // driver program
    public static void main(String args[])
    {
        String s = "Ravi had been saying that he had been there";
        String firstWord = findFirstRepeated(s);
        if (!firstWord.equals("NoRepetition"))
            System.out.println("First repeated word :: " + firstWord);
        else
            System.out.println("No Repetitionn");
    }
}


Python3
# Python program for the above approach
from collections import Counter
 
# Python program to find the first
# repeated character in a string
def firstRepeatedWord(sentence):
 
    # splitting the string
    lis = list(sentence.split(" "))
     
    # Calculating frequency of every word
    frequency = Counter(lis)
     
    # Traversing the list of words
    for i in lis:
       
        # checking if frequency is greater than 1
         
        if(frequency[i] > 1):
            # return the word
            return i
 
 
# Driver code
sentence = "Vikram had been saying that he had been there"
print(firstRepeatedWord(sentence))
# this code is contributed by vikkycirus


Python3
# Import defaultdict from Collections module
from collections import defaultdict
 
def first_repeating_word(s):
 
    # Creating a defaultdict with
    # default values as 0.
    # Every word will have an
    # initial count of 0
    word_count = defaultdict(lambda: 0)
 
    # Iterating through all words in string.
    for i in s.split():
 
        # Increment the word count of
        # the word we encounter by 1
        word_count[i] += 1
 
        # If word_count of current word
        # is more than 1, we got our answer, return it.
        if word_count[i] > 1:
            return i
 
    # If program has reached here that
    # means no word is being repeated
    return 'No word is being repeated'
 
 
# Driver Code
if __name__ == '__main__':
    s = "Ravi had been saying that he had been there"
    print(first_repeating_word(s))
 
    # This code is contributed by Anurag Mishra


Java
// Java program for finding first repeated
// word in a string
import java.util.*;
 
public class GFG{
     
    // returns first repeated word
    static String findFirstRepeated(String s)
    {
        // break string into tokens
        String token[] = s.split(" ");
         
        // hashset for storing words
        HashSet set = new HashSet();
         
        // store the words of string in hashset
        for(int i=0; i


C++
// CPP program for finding first repeated
// word in a string
#include 
using namespace std;
 
// returns first repeated word
string findFirstRepeated(string s)
{
    // break string into tokens
    // and then each string into set
    // if a word appeared before appears
    // again, return the word and break
 
    istringstream iss(s);
    string token;
 
    // hashset for storing word and its count
    // in sentence
    set setOfWords;
 
    // store all the words of string
    // and the count of word in hashset
 
    while (getline(iss, token, ' ')) {
        // if word exists return
        if (setOfWords.find(token) != setOfWords.end()) {
            return token;
        }
 
        // insert new word to set
        setOfWords.insert(token);
    }
 
    return "NoRepetition";
}
 
// driver program
int main()
{
    string s("Ravi had been saying that he had been there");
    string firstWord = findFirstRepeated(s);
    if (firstWord != "NoRepetition")
        cout << "First repeated word :: " << firstWord
             << endl;
    else
        cout << "No Repetitionn";
    return 0;
}


输出
had
No Repetition
he

另一种方法:这个想法是对字符串进行标记并将每个单词及其计数存储在 hashmap 中。然后再次遍历字符串,对于字符串的每个单词,检查它在创建的 hashmap 中的计数。

CPP

// CPP program for finding first repeated
// word in a string
#include 
using namespace std;
 
// returns first repeated word
string findFirstRepeated(string s)
{
    // break string into tokens
    // and then each string into set
    // if a word appeared before appears
    // again, return the word and break
 
    istringstream iss(s);
    string token;
 
    // hashmap for storing word and its count
    // in sentence
    unordered_map setOfWords;
 
    // store all the words of string
    // and the count of word in hashmap
 
    while (getline(iss, token, ' ')) {
        if (setOfWords.find(token) != setOfWords.end())            
            setOfWords[token] += 1;  // word exists
        else
            // insert new word to set
            setOfWords.insert(make_pair(token, 1));       
    }
 
    // traverse again from first word of string s
    // to check if count of word is greater than 1
 
    // either take a new stream or store the words
    // in vector of strings in previous loop
    istringstream iss2(s);
    while (getline(iss2, token, ' ')) {
        int count = setOfWords[token];
        if (count > 1) {
            return token;
        }
    }
 
    return "NoRepetition";
}
 
// driver program
int main()
{
    string s("Ravi had been saying that he had been there");
    string firstWord = findFirstRepeated(s);
    if (firstWord != "NoRepetition")
        cout << "First repeated word :: "
             << firstWord << endl;
    else
        cout << "No Repetitionn";
    return 0;
}

Java

// Java program for finding first repeated
// word in a string
import java.util.*;
 
class GFG{
     
    // returns first repeated word
    static String findFirstRepeated(String s)
    {
        // break string into tokens
        // and then each string into set
        // if a word appeared before appears
        // again, return the word and break
     
        String token[] = s.split(" ");
     
        // hashmap for storing word and its count
        // in sentence
        HashMap setOfWords = new HashMap();
     
        // store all the words of string
        // and the count of word in hashmap
     
        for (int i=0; i 1) {
                return token[i];
            }
        }
     
        return "NoRepetition";
    }
     
    // driver program
    public static void main(String args[])
    {
        String s = "Ravi had been saying that he had been there";
        String firstWord = findFirstRepeated(s);
        if (!firstWord.equals("NoRepetition"))
            System.out.println("First repeated word :: " + firstWord);
        else
            System.out.println("No Repetitionn");
    }
}
输出
First repeated word :: had

方法#2:使用内置的Python函数:

  • 因为句子中的所有单词都用空格分隔。
  • 我们必须使用split() 将句子按空格分隔。
  • 我们用空格分割所有单词并将它们存储在一个列表中。
  • 使用Counter函数计算单词的频率
  • 遍历列表并检查是否有任何单词的频率大于 1
  • 如果存在,则打印该单词并中断循环

下面是实现:

Python3

# Python program for the above approach
from collections import Counter
 
# Python program to find the first
# repeated character in a string
def firstRepeatedWord(sentence):
 
    # splitting the string
    lis = list(sentence.split(" "))
     
    # Calculating frequency of every word
    frequency = Counter(lis)
     
    # Traversing the list of words
    for i in lis:
       
        # checking if frequency is greater than 1
         
        if(frequency[i] > 1):
            # return the word
            return i
 
 
# Driver code
sentence = "Vikram had been saying that he had been there"
print(firstRepeatedWord(sentence))
# this code is contributed by vikkycirus
输出
had

优化方法:

我们可以在任何单词的计数变为 2 时停止,而不是计算每个单词的出现次数,这将具有 O(N) 时间和空间复杂度,其中 N 是单词的数量。这不需要遍历所有字符串中的单词。

假设我们的第一个重复单词出现在第 M 个索引处,那么

通过使用这种方法,空间和时间复杂度从 O(N) 降低到 O(M)。

在哪里,

N:字符串中的单词数。

M:第一个重复词出现的索引

然而,最坏的情况(当没有单词被重复或被重复的单词最后出现时)时间和空间复杂度仍然是 O(N)。

脚步:

  • 创建一个初始值为 0 的默认字典,以跟踪单词数。
  • 遍历句子中的每个单词并将该单词的计数加 1。
  • 如果(单词的计数)> 1,则返回单词。
  • 如果没有一个单词的计数大于 1,那么这就是我们在循环之外,然后返回“没有单词被重复”。

Python3

# Import defaultdict from Collections module
from collections import defaultdict
 
def first_repeating_word(s):
 
    # Creating a defaultdict with
    # default values as 0.
    # Every word will have an
    # initial count of 0
    word_count = defaultdict(lambda: 0)
 
    # Iterating through all words in string.
    for i in s.split():
 
        # Increment the word count of
        # the word we encounter by 1
        word_count[i] += 1
 
        # If word_count of current word
        # is more than 1, we got our answer, return it.
        if word_count[i] > 1:
            return i
 
    # If program has reached here that
    # means no word is being repeated
    return 'No word is being repeated'
 
 
# Driver Code
if __name__ == '__main__':
    s = "Ravi had been saying that he had been there"
    print(first_repeating_word(s))
 
    # This code is contributed by Anurag Mishra
输出
had

时间复杂度:O(M)

空间复杂度:O(M)

优化方法二:

我们可以只将单词存储在 HashSet 中,一旦我们找到一个已经存在于我们可以返回的 HashSet 中。

Java

// Java program for finding first repeated
// word in a string
import java.util.*;
 
public class GFG{
     
    // returns first repeated word
    static String findFirstRepeated(String s)
    {
        // break string into tokens
        String token[] = s.split(" ");
         
        // hashset for storing words
        HashSet set = new HashSet();
         
        // store the words of string in hashset
        for(int i=0; i

C++

// CPP program for finding first repeated
// word in a string
#include 
using namespace std;
 
// returns first repeated word
string findFirstRepeated(string s)
{
    // break string into tokens
    // and then each string into set
    // if a word appeared before appears
    // again, return the word and break
 
    istringstream iss(s);
    string token;
 
    // hashset for storing word and its count
    // in sentence
    set setOfWords;
 
    // store all the words of string
    // and the count of word in hashset
 
    while (getline(iss, token, ' ')) {
        // if word exists return
        if (setOfWords.find(token) != setOfWords.end()) {
            return token;
        }
 
        // insert new word to set
        setOfWords.insert(token);
    }
 
    return "NoRepetition";
}
 
// driver program
int main()
{
    string s("Ravi had been saying that he had been there");
    string firstWord = findFirstRepeated(s);
    if (firstWord != "NoRepetition")
        cout << "First repeated word :: " << firstWord
             << endl;
    else
        cout << "No Repetitionn";
    return 0;
}
输出
First repeated word :: had