What is a word called that consists of a repetition of one word?
I came across the word polypoly in one of the other question asked on the site, and it got me thinking whether there are other English words that are made up of a word or part of a word repeated in the same way that polypoly is made up of poly + poly. Can you please provide examples also?
NVZ♦
22.5k30 gold badges68 silver badges122 bronze badges
asked May 21, 2014 at 22:47
5
This is called reduplication. If you repeat the whole word (or lexeme), it is called full reduplication. There is usually a hyphen in between. (For example: go-go)
There is a list of English reduplications here:
http://en.wiktionary.org/wiki/Category:English_reduplications
Full reduplication examples:
- boo-boo
- bye-bye
- cancan
- chop-chop
- gee-gee
- jaw-jaw
- licky-licky
- moo-moo
- murmur
- nulla-nulla
- pee-pee
- pompom
- poo-poo
- pooh-pooh
- rah-rah
- tartar
- dodo
- lulu
- tutu
- juju
- papa
- couscous
- tete
- coco
- dik-dik
- beriberi
answered May 21, 2014 at 23:04
ermanenermanen
59k34 gold badges159 silver badges291 bronze badges
14
To supplement ermanen’s list, I note these from Merriam-Webster’s Eleventh Collegiate Dictionary (2003):
ack-ack (n.)
agar-agar (n.)
atlatl (n.)
aye-aye (n.)
Berber (n.)
bonbon (n.)
buddy-buddy (adj.)
bulbul (n.)
caracara (n.)
cha-cha (n.)
chowchow (n.—the relish not the dog, which is spelled chow chow)
dumdum (n.—the bullet)
dum-dum (n.—the nitwit)
gaga (adj.)
go-go (adj.)
goody-goody (adj.)
goo-goo (adj. & n.)
gris-gris (n.)
ha-ha (interj.)
ha-ha (n.—a sunk fence)
lavalava (n.—has nothing to do with volcanic lava)
mau-mau (v.)
meme (n.)
motmot (n.)
mumu (n., from MW’s Unabridged dictionary)
no-no (n.)
pawpaw (n.)
so-so (adj. & adv.)
tsetse (n.)
tut-tut (interj. & v.)
For some reason, Merriam-Webster’s doesn’t acknowledge the reality of hubba-hubba. But I’m sure that there are others in the dictionary that I’ve missed…
answered May 22, 2014 at 1:16
Sven YargsSven Yargs
157k34 gold badges406 silver badges726 bronze badges
2
Such a word is a tautonym. See sense 2 here: «(linguistics) A word or term made from two identical parts or syllables, such as bonbon or dada.»
answered Apr 2, 2022 at 6:30
Rosie FRosie F
4,8631 gold badge14 silver badges25 bronze badges
Definition
Word Formation Process (also called Morphological Process) is a means by which new words are produced either by modification of existing words or by complete innovation, which in turn become a part of the language.
Types of Word Formation Processes
Different types of word formation processes are employed to create new words. However, all word formation processes basically bring either inflectional or derivational changes. Therefore, inflection (also called inflexion) and derivation are the two core processes of word formation. Inflection differs from derivation to the following extent:
Inflection | Derivation |
Produces grammatical variants of the same word. | Produces a new word on the basis of an existing word. |
Modifies a word to express different grammatical categories such as tense, mood, voice, aspect, person, number, gender and case. | Changes the word class (also called parts of speech; form class; lexical class; syntactic category). |
Does not change the meaning of a word. For example: determine→ determines, determining, determined. | Modifies the meaning of the root. For example: modern → modernize (to make modern). |
The major word formation processes include but are not limited to the following:
Affixation
It is a word formation process wherein an affix is attached to a root (also called stem; base) to form a new word. A root is a free morpheme (also called unbound morpheme) that can appear alone. On the other hand, an Affix is a bound morpheme which never occurs by itself, but is always attached to some free morpheme and can be either inflectional or derivational. An Inflectional affix modifies the form/grammatical category of a word, i.e., tense, person, number, gender, case, etc. For example: rat → rats. Contrariwise, a derivational affix modifies the parts of speech of the root, while leaving the grammatical category unchanged. In this way, there is a change of meaning of the root. For example: write → writer.
In English there are two types of affixations:
- Prefixation: In this morphological process words are formed by adding an affix to the front of a root. The type of affix used in this process is referred to as prefix. For example: un + tidy → untidy
- Suffixation: In this morphological process words are formed by adding an affix to the end of a root. The type of affix used in this process is referred to as suffix. For example: fear + less → fearless
Conversion
This refers to the change of function or parts of speech of a word without adding an affix. Conversion is also called zero derivation or null derivation since the functional change is brought about by supplementing an invisible affix. Sometimes it is also called functional shift. Typically conversion is made from “noun to verb” and from “verb to noun”. Less frequently, conversion is also done from “adjective to verb” and “adjective to noun”. For instance:
Noun to Verb:
- access
- film
- name
- shape
Verb to Noun:
- attack
- alert
- hope
- increase
- visit
- cover
Adjective to Verb:
- brown
- black
- slow
Adjective to Noun:
- crazy
- nasty
Back-formation
Back-formation is a morphological process in which new word is created by extracting affixes from another word. In this way, it is the reverse of affixation, in which affixes are added. Back-formation is also different from clipping since it brings a change in the parts of speech or the word’s meaning. For example: the noun insertion has been back-formed into verb insert by removing the suffix ion.
Clipping
As the name suggests, clipping is the word formation process in which a word is reduced to a shorter form. With a sharp contrast to back-formation, clipping keeps the original word meaning intact. These words are very common in everyday speech. For instance: lab is the clipped form of laboratory. . There are four types of clippings:
- Back clipping: (also called final clipping; apocope) it involves the truncation of end of a word as in ad from advertisement.
- Fore-clipping: (also called initial clipping; apheresis) it is the removal of the beginning of a word as in phone from telephone.
- Middle clipping: (also medial clipping; syncope) it is the extraction of the beginning and end of a word as in flu from influenza.
- Complex clipping: is removing multiple parts from multiple words as in cablegram from cabletelegram.
Compounding
Also called composition, by this process two or more than two words are combined together to create a single word, having a single idea and function. In English, there are compound nouns, compound adjectives, and compound verbs. Customarily compound words are spelt as a single word, or as two or more hyphenated words, and even as two or more separate words. For example:
- life + style → lifestyle
- mother + in + law → mother-in-law
- shopping + mall → shopping mall
There are no specific rules for hyphenated compounds. Generally, some new and original compound nouns are hyphenated, but the hyphen is ignored when they become more familiar. However, there are some compound adjectives that are always hyphenated. For instance: state-of-the-art. The hyphen is often retained when two vowels come together, such as: Co-operation. Hyphens are often used to tell the ages of people and things, for example: 10-year-old. The general rule is that words are combined with hyphens to avoid confusion.
Borrowing
This refers to the words adopted from other languages. There are two types of borrowings:
- Loan-word: By this process, a word is borrowed from another language without translating it into the target language. For example: the phrase tour-de-force is borrowed directly from French, which means a masterly or brilliant feat.
- Loan-translation: Also known as calque, a morphological process wherein a word or phrase from another language is borrowed by literally translating it into the target language. For example: the phrase point of view has been translated into English from the French phrase point de vue.
Coinage
Also called invention, is a morphological process by which new words are invented. Sometimes popular trademark names of various products are adopted by people so extensively that they ultimately become the everyday words of language. For example:
- Heroin
- Aspirin
- Escalator
- Xerox
- Kerosene
- Nylon
- Band-Aid
- Vaseline
- Margarine
- Videotape
Again, some words are being invented due to rapid cultural changes and the spread of information technology, mass media, internet, etc. For example:
- Blog
- Hotspot
- Netbook
- Tablet
- Tweet
- Emoticon
- Smartphone
Blending
Blending (also called portmanteau) is a morphological process in which the parts of two or more words are combined together to form a new word. Usually, the parts consist of the beginning of one word and the end of the other word(s). Typically, the meaning of the blended word reverberates with the meanings of the original words. For example:
- breakfast + lunch → brunch
- motor+hotel → motel
However, blending should not be confused with compounding, which combines two words without truncation of parts of the roots of the blended words.
Acronyms
These words are formed with the initial letters or each of the major parts of a word or a longer phrase. With a few exceptions, acronyms are usually capitalized. Some linguists confuse acronyms with initialisms, which are also abbreviations formed in the similar manner as the former. In essence, there is a sharp difference between the two. In language, an acronym is pronounced as a single word rather than just a sequence of individual letters, which is characteristic of initialisms. For example:
Acronyms:
- United Nations Educational, Scientific and Cultural Organization → UNESCO
- Light Amplification by Stimulated Emission of Radiation → Laser
- International Criminal Police Organization → Interpol
Initialisms:
- Personal Computer → PC
- Asian Development Bank → ADB
- Liquid Crystal Display → LCD
Reduplication
Reduplication (also called cloning; doubling; duplication; repetition; tautonym) is a word formation process in which a new word is created by repeating all or part of a root or a stem, often with a change of vowel or initial consonant. Reduplication is not a major means of creating lexemes in English, but it is perhaps the most unusual one. Based on their usage, the techniques of reduplication could be classified in the following manner:
- Repetition without Change: bye-bye, tick-tick
- Rhyming Reduplication: ding-dong, super-duper, bow-wow
- Repetition with Change of Vowel: tiptop, chitchat, flip-flop, ping-pong, dilly-dally, wishy-washy
- Repetition with Change of Initial Consonant: teeny-weeny
References
“English Word Formation Processes.” Really Learn English. 2016. Really-Learn-English.com.
14 July 2016 <http://www.really-learn-english.com/word-formation-processes.html>.
“Inflection.” Wikipedia. 2016. Wikimedia Foundation Inc. 14 July 2016
<http://en.wikipedia.org/wiki/Inflection>.
“Morphological Derivation.” Wikipedia. 2016. Wikimedia Foundation Inc. 14 July 2016
<http://en.wikipedia.org/wiki/Morphological_derivation>.
Yule, George. The Study of Language. 2nd ed. Cambridge: CUP, 1996.
“Word Formation.” Wikipedia. 2016. Wikimedia Foundation Inc. 14 July 2016
<https://en.wikipedia.org/wiki/Word_formation>.
In the word cloud I have repetitive words and I do not understand why they are not counted together and are shown then as one word.
from wordcloud import WordCloud
word_string = 'oh oh oh oh oh oh verse wrote book stand title book would life superman thats make feel count privilege love ideal honored know feel see everyday things things say rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock love rock oh oh oh verse try count ways make smile id run fingers run timeless things talk sugar keeps going make wanna keep lovin strong make wanna try best give want need give whole heart little piece minimum talking everything single wish talking every dream rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock wanna rock bridge theres options dont want theyre worth time cause oh thank like us fine rock sand smile cry joy pain truth lies matter know count oh oh oh oh oh oh rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock love rock oh oh oh oh oh oh wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock wanna rock party people people party popping sitting around see looking looking see look started lets hook little one one come give stuff let freshin ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture party people people party popping sitting around see looking looking see look started lets hook come one give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture lets hook come give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go lets hook come give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture black culture black culture black culture black culture'
wordcloud = WordCloud(background_color="white",
width=1200, height=1000,
stopwords=STOPWORDS
).generate(word_string)
plt.imshow(wordcloud)
As you see words like love, oh, rock, black, culture appear several times and it seems that they are not counted together. What am I doing wrong?
micstr
4,9367 gold badges48 silver badges75 bronze badges
asked May 13, 2017 at 14:10
3
That is a feature called ‘collocations’ in the word_cloud project. You can turn it off by setting collocations=False, like this:
wordcloud = WordCloud(collocations=False).generate(word_string)
This will get rid of words that are frequently grouped together in your text. It will get rid of some things you probably don’t like, for instance, «oh oh» and it will get rid of some others that you may like, for instance, «black culture»
answered Jul 14, 2017 at 4:44
craigchingcraigching
1,5432 gold badges13 silver badges12 bronze badges
2
If you look at wordcloud.words_
you will see the frequency table includes some two-word phrases like ‘oh oh’, ‘hook start’, ‘lets go’, ‘lets hook’.
You would need to dig into the code behind .process_text()
to see exactly why it does this.
As a work-around you could split word_string
yourself to build a word-frequency table, then use .generate_from_frequencies()
to create the image.
answered May 13, 2017 at 14:43
Hugh BothwellHugh Bothwell
54.7k8 gold badges84 silver badges99 bronze badges
When writing, even the most expert makes mistakes. Both in writing and in style. On many occasions the same program corrects them, but sometimes you have to help it, as is the case of finding all the repeated words in a Word document .
When you review your document, no matter how many times you read it, you may not notice all the errors that are in it. Similarly, the automatic corrector is not always as effective as it is thought. Don’t worry, Word has the necessary tools to correct your text and leave it without a single error. You just have to learn how to use it to get the most out of it.
How to Find All Repeated Words in a Word Document
It is necessary to know how many versions of Windows 10 there are, to determine if you have the most recent and therefore the most current edition of Word.
Index( )
- What is called the repetition of words in a text
- Find all repeated words in a Word document
- Word wildcards
- How to easily check repeated words in Word
- Find repeating paragraphs in Word
- Repeated words in a Word document and wildcards
- How to underline all the same words in Word
- How to delete the same words in Word
- How to find all the repeated words in a Word document using wildcards?
What is called the repetition of words in a text
There is no specific term for this problem, but we could use ‘redundancy’ or ‘catchphrase’. Redundancy refers to the tendency to extend an argument too long and is something that should be avoided in certain cases. On the other hand, the catchphrase is the use of phrases in a repetitive way.
Although the catchphrase is seen mainly in speeches, it can also sneak into our texts, if we tend to use a word too much. Redundancy serves a purpose if you want to express a complex message in an explainable way, but you have to be careful and the catchphrase should also be avoided at all costs .
One of the most common mistakes when writing a document is that sometimes repeated words can be written. Normally, Word’s auto-checker alerts you to this error by underlining repetition in red. But this is not infallible, if you have the correction tool disabled it is still possible to detect these errors very easily. Using the program’s wildcards.
Word wildcards
Wildcards in Word are quite useful tools when navigating and correcting a document. However, many users do not know how to use them and many more do not even know that they exist. To find all the repeated words in a Word document, you can make use of these wildcards and even lock the position of an image in Word.
How to easily check repeated words in Word
Without a doubt, the easiest way to find repeated words in a Word document is by using the underline tool. With this tool you can check the integrity of your text looking for words that you think you repeated a lot. We will show you this below.
Find repeating paragraphs in Word
You may have gotten confused and copied a paragraph into large text. Fortunately, you can find repeating paragraphs if you use the underline tool below. It is enough that, instead of writing a word, you paste the text that is repeated in the document .
Repeated words in a Word document and wildcards
To get a repeated word, the first thing to do is search for a word, right? For this we will use the Word tool ” Search “. This is in the ” Start ” section of the top menu. It should be noted that to use this type of search you must select the “Use wildcard characters” box in the Search and Replace window.
If a word is a sequence of letters arranged in a specific way. So the first wildcard to use would be [a-zA-Z]. In this way the search engine will consider all the letters of the alphabet taking into account upper and lower case. Similarly, within this sequence will be the vowels with their accents and the letter “ñ”.
With that joker you can already get a letter. Now, how to find a word? Well, to do that you must use this wildcard [aaZ] {1;}. This is translated as a redundant letter one or more times, counting capital letters, accents, and the letter ñ.
The <> characters placed at the beginning and end of the wildcard will indicate the beginning and end of each word. But since you want to search for repeated words, you will modify the wildcard to the following <[a-zA-Z] {1;}). By replacing the “greater than” characters with a parenthesis, you are saying that you want to search for words followed by a space.
How to underline all the same words in Word
Underlining words is simple, for this we must use the ‘Find and replace’ tool. This option is found in the ‘Home’ section with the name of ‘Replace’, in turn, you can press Ctrl + B to display the tool.
Make sure you are in the ‘Search’ tab and type the word you want to search for. At the top you will see the option ‘Highlight reading’ , There select ‘Highlight all’. Doing the above will highlight the word in the text so you can see all the times it is there.
How to delete the same words in Word
To eliminate the repeated words, we must also use the ‘Find and replace’ tool . Remember, you access it through ‘Start’, there click on ‘Replace’. The tool menu will be displayed, you must click on the ‘Replace’ tab.
In ‘Search’ write the term you want to remove from your text and leave the ‘Replace with’ section blank. You can do the process one by one, clicking on ‘Find next’ to see the word in each section of the text, to make the modification click on ‘Replace’. If you want to remove all the words , click on ‘Replace all’.
How to find all the repeated words in a Word document using wildcards?
As an extra point, you should know that wildcards also apply to other cases, such as inserting a YouTube video in a Word document , a useful tool to finalize the file together with the elimination of repeated words.
Now, if the previous wildcard only searches for words followed by a space , how to find all the repeated words in a Word document? For that case, the wildcard explained above would be modified to the following (<[a-zA-Z] {1;}) 1>.
If you can tell, the previous wildcard is now inside parentheses. In wildcard theory it says that parentheses are used to create search references. Therefore adding parentheses created a reference.
This is followed by a space and then 1 follows. This refers to the word to search for. That is, to the first group of parentheses. If there were more sets of characters the reference would be 2, 3, or the amount of group of parentheses that are.
And finally this character> refers to the end of the word. So if you use this wildcard, it will find the redundancy “his his” but not “his dream” . Finally, after finding all the repeated words in a Word document, you will want to delete all the repeated words.
Thus, in the Word search and replace window you must put the following wildcard in the search bar (<[a-zA-Z] {1;}) 1> and in the replace window you must put 1. In this way, the repeated word will be deleted automatically.
Word Unscrambler is a tool specifically created to help you find the highest-scoring words for Scrabble, Words with Friends, and other word games. By entering your current letter tiles, Word Unscrambler’s unique search engine will suggest all valid words from the selection given.
Word Unscrambler — Definition and Examples
Word Unscrambler helps you to find the best cheats and highest scoring words for Scrabble, Words with Friends and many other word games.
When playing Words with Friends or Scrabble, you can come across tricky tiles. No matter our skill level, it’s sometimes useful to make use of a tool like unscramble and get a fresh perspective on all playable words.
What is the Word Unscrambler Tool?
In a nutshell, a word unscrambler is a tool that you enter all your letters in your hand and it rearranges them to reveal all possible word combinations.
Some people may worry that this is a way to cheat. However, if all game participants have an option to use a word unscrambler, then there’s certainly an even playing field. A player may decide not to use the unscrambling tool and come up with words on their own. Having said that, they might want to use it afterwards to test themselves and see the full list of potential words that they could have played.
How to Use Word Unscramblers
Simply enter the tiles you are struggling with and we will unscramble the letters into something that not only makes sense but will also reward you with the highest score possible. Think of us as a helping hand that also helps boost your mental dexterity and vocabulary. A bit of jumble solving each day helps you become a top word unscrambler!
Benefits of Using WordTips Word Unscramble
As you can see, there are different ways that a word descrambling device can be employed. And, there are no hard and fast rules about when to use one. What’s more, word unscramblers can be useful in board games like Scrabble and Words with Friends as well as crossword puzzles and games like hangman or Word A Round ─ virtually any word game that you can think of. You can even enjoy using it while playing along at home with a word-based TV game show!
Now that you know a little bit about it, are you interested in some examples of how to use the tool and the benefits it gets you? Here’s what we have for you:
A. Win Word Games
Player A is a Scrabble participant who is baffled by how to get the highest score from the following scrambled letters on their rack ─ ERIKNRG.
When they enter the letters into the word descrambler, it shows a number of words using two or more of the letters. The highest points ─ 15 ─ are for the word GHERKIN that uses all seven letters, not a word that may ordinarily come to mind quickly!
B. Boost Your Vocabulary
Player B is a young person playing Word A Round (a game for ages 10 and up) and they’re trying to be the first to unravel the following scrambled letters around the game card ─ LANIMA (6-letter word), ULHELPF (7-letter word) and RELSQUIR (8-letter word).
By using a word unscrambler, they’ll find these words ─ ANIMAL, HELPFUL and SQUIRREL. Any boost that you can give a child while they’re learning how to play will encourage a love of the game. In turn, they will be excited to try to win and want to play more. This will really enlarge their vocabulary!
Letters vs. Words
Unscramble Letters
Working with such a device can definitely be of benefit when attempting to unscramble letters to make words. Furthermore, our Word Unscrambler is a great word solver. It will accommodate up to 15 letters and locate a truly amazing array of words using all manner of combinations of vowels and constants. You can also use the advanced search to find words that begin or end with specific letters. And, that’s not all! The Word Unscrambler can be of service when you want to check out words that contain certain letters or see words with letters in a particular position. If you need to select words using a distinct dictionary, we’ve got that covered too by including all references that you may need.
Unscramble Words
A word unscrambling tool can, of course, be a support to unscramble jumbled words. The English language is fascinating in its variety. Spellings are not always very intuitive. Silent letters appear and pronunciation emphasis on different syllables can be confusing. It’s said to be one of the most difficult languages in the world to learn! Also, there are words that sound the same but are spelled using different letters and have totally unrelated definitions. Therefore, having our Word Unscrambler at your fingertips can be a real plus when you’re attempting to sort out what words the mixed-up letters reveal.
6 Tips and Tricks to Unscramble Words
There are a number of tips and tricks that can be beneficial to unscramble words from jumbled letters. Everyone has their favorites ─ maybe tried and true ones that have worked for them in the past to make words or some that they find quick and easy to use. Following are some tips and tricks that we suggest to help you find the answers to the puzzle of letters you have before you.
- Separate the consonants from the vowels.
- Try to match various consonants with vowels to see what you come up with. All words need to have vowels. Also, while you can have a word with just one vowel, such as “A” or “I”, consonants cannot stand on their own.
- Look for short words to start with such as those with 2 or 3 letters. Then, find out if you can lengthen these by pluralizing them or adding any letters you have that can change the tense.
- Pick our any prefixes or suffixes that can extend the length of the words you come up with.
- Play with a pencil and paper to create a list of possible words. Make sure to check the spelling to ensure that you haven’t just made up a non-existent word!
- If you’re playing a word game with tiles, move them around to see if a word materializes when you look at different letter combinations.
Top 10 Most Popular Unscrambling Examples
Now that you’re well on your way to understanding what you need to know about word unscrambler tools, you’re probably itching to try out our Word Unscrambler! Before you get going, let us show you some of the most popular unscrambling examples. We’ve focused on 7-letter words here since that’s the number of tiles you have in two of the most well-known word games ─ Scrabble and Words with Friends.
- EE CFRPT becomes PERFECT
- AU BDHNS becomes HUSBAND
- AEE CHTR becomes TEACHER
- EEI CCNS becomes SCIENCE
- AEI CCLPS becomes SPECIAL
- AOU LPPR becomes POPULAR
- AE PRE MD becomes PREMADE
- ING O NSW becomes SNOWING
- RE EO DNZ becomes REZONED
- AOE SMEW becomes AWESOME
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
question source : https://www.geeksforgeeks.org/goldman-sachs-interview-experience-set-29-internship/
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:
C++
#include<bits/stdc++.h>
using
namespace
std;
void
solve(string s)
{
unordered_map<string,
int
> mp;
string t=
""
,ans=
""
;
for
(
int
i=s.length()-1;i>=0;i--)
{
if
(s[i]!=
' '
)
{
t+=s[i];
}
else
{
mp[t]++;
if
(mp[t]>1)
ans=t;
t=
""
;
}
}
mp[t]++;
if
(mp[t]>1)
ans=t;
if
(ans!=
""
)
{
reverse(ans.begin(),ans.end());
cout<<ans<<
'n'
;
}
else
cout<<
"No Repetitionn"
;
}
int
main()
{
string u=
"Ravi had been saying that he had been there"
;
string v=
"Ravi had been saying that"
;
string w=
"he had had he"
;
solve(u);
solve(v);
solve(w);
return
0;
}
Java
import
java.util.*;
public
class
GFG {
public
static
void
solve(String s)
{
HashMap<String, Integer> mp
=
new
HashMap<String,
Integer>();
String t =
""
;
String ans =
""
;
for
(
int
i = s.length() -
1
; i >=
0
; i--) {
if
(s.charAt(i) !=
' '
) {
t += s.charAt(i);
}
else
{
if
(!mp.containsKey(t)) {
mp.put(t,
1
);
}
else
{
mp.put(t, mp.get(t) +
1
);
}
if
(mp.get(t) >
1
) {
ans = t;
}
t =
""
;
}
}
if
(!mp.containsKey(t)) {
mp.put(t,
1
);
}
else
{
mp.put(t, mp.get(t) +
1
);
}
if
(mp.get(t) >
1
) {
ans = t;
}
if
(!ans.equals(
""
)) {
StringBuilder input1 =
new
StringBuilder();
input1.append(ans);
input1.reverse();
System.out.println(input1);
}
else
{
System.out.print(
"No Repetitionn"
);
}
}
public
static
void
main(String[] args)
{
String u
=
"Ravi had been saying that he had been there"
;
String v =
"Ravi had been saying that"
;
String w =
"he had had he"
;
solve(u);
solve(v);
solve(w);
}
}
Python3
def
solve(s):
mp
=
{}
t
=
""
ans
=
""
for
i
in
range
(
len
(s)
-
1
,
-
1
,
-
1
):
if
(s[i] !
=
' '
):
t
+
=
s[i]
else
:
if
(t
in
mp):
ans
=
t
else
:
mp[t]
=
1
t
=
""
if
(t
in
mp):
ans
=
t
if
(ans!
=
""):
ans
=
ans[::
-
1
]
print
(ans)
else
:
print
(
"No Repetition"
)
u
=
"Ravi had been saying that he had been there"
v
=
"Ravi had been saying that"
w
=
"he had had he"
solve(u)
solve(v)
solve(w)
C#
using
System;
using
System.Collections.Generic;
class
GFG {
static
void
solve(
string
s)
{
Dictionary<
string
,
int
> mp =
new
Dictionary<
string
,
int
>();
string
t =
""
;
string
ans =
""
;
for
(
int
i = s.Length - 1; i >= 0; i--) {
if
(s[i] !=
' '
) {
t += s[i];
}
else
{
if
(mp.ContainsKey(t)) {
mp[t] += 1;
}
else
{
mp.Add(t, 1);
}
if
(mp[t] > 1) {
ans = t;
}
t =
""
;
}
}
if
(mp.ContainsKey(t)) {
mp[t] += 1;
}
else
{
mp.Add(t, 1);
}
if
(mp[t] > 1) {
ans = t;
}
if
(ans !=
""
) {
char
[] charArray = ans.ToCharArray();
Array.Reverse(charArray);
Console.WriteLine(
new
string
(charArray));
}
else
{
Console.Write(
"No Repetitionn"
);
}
}
public
static
void
Main()
{
string
u
=
"Ravi had been saying that he had been there"
;
string
v =
"Ravi had been saying that"
;
string
w =
"he had had he"
;
solve(u);
solve(v);
solve(w);
}
}
Javascript
<script>
function
solve(s)
{
let mp =
new
Map();
let t =
""
;
let ans =
""
;
for
(let i = s.length - 1; i >= 0; i--)
{
if
(s[i] !=
' '
)
{
t += s[i];
}
else
{
if
(mp.has(t))
ans = t;
else
mp.set(t, 1)
t =
""
;
}
}
if
(mp.has(t)) ans=t;
if
(ans!=
""
)
{
ans = [...ans].reverse().join(
""
);
document.write(ans);
}
else
document.write(
"No Repetition"
);
}
const u =
"Ravi had been saying that he had been there"
;
const v =
"Ravi had been saying that"
;
const w =
"he had had he"
;
solve(u);
solve(v);
solve(w);
</script>
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:
CPP
#include <bits/stdc++.h>
using
namespace
std;
string findFirstRepeated(string s)
{
istringstream iss(s);
string token;
unordered_map<string,
int
> setOfWords;
while
(getline(iss, token,
' '
)) {
if
(setOfWords.find(token) != setOfWords.end())
setOfWords[token] += 1;
else
setOfWords.insert(make_pair(token, 1));
}
istringstream iss2(s);
while
(getline(iss2, token,
' '
)) {
int
count = setOfWords[token];
if
(count > 1) {
return
token;
}
}
return
"NoRepetition"
;
}
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
import
java.util.*;
class
GFG{
static
String findFirstRepeated(String s)
{
String token[] = s.split(
" "
);
HashMap<String, Integer> setOfWords =
new
HashMap<String, Integer>();
for
(
int
i=
0
; i<token.length; i++) {
if
(setOfWords.containsKey(token[i]))
setOfWords.put(token[i], setOfWords.get(token[i]) +
1
);
else
setOfWords.put(token[i],
1
);
}
for
(
int
i=
0
; i<token.length; i++) {
int
count = setOfWords.get(token[i]);
if
(count >
1
) {
return
token[i];
}
}
return
"NoRepetition"
;
}
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
class
GFG:
@staticmethod
def
findFirstRepeated(s):
token
=
s.split(
" "
)
setOfWords
=
{}
for
i
in
range
(
len
(token)):
if
token[i]
in
setOfWords:
setOfWords[token[i]]
+
=
1
else
:
setOfWords[token[i]]
=
1
for
i
in
range
(
len
(token)):
count
=
setOfWords[token[i]]
if
count >
1
:
return
token[i]
return
"NoRepetition"
@staticmethod
def
main(args):
s
=
"Ravi had been saying that he had been there"
firstWord
=
GFG.findFirstRepeated(s)
if
firstWord !
=
"NoRepetition"
:
print
(
"First repeated word :: "
+
firstWord)
else
:
print
(
"No Repetition"
)
GFG.main([])
C#
using
System;
using
System.Collections.Generic;
using
System.Collections;
using
System.Linq;
class
HelloWorld {
public
static
string
findFirstRepeated(
string
s)
{
string
[] token = s.Split(
" "
);
Dictionary<
string
,
int
> setOfWords =
new
Dictionary<
string
,
int
>();
for
(
int
i=0; i < token.Length; i++)
{
if
(setOfWords.ContainsKey(token[i]) ==
true
)
{
setOfWords[token[i]] = setOfWords[token[i]] + 1;
}
else
{
setOfWords.Add(token[i], 1);
}
}
for
(
int
i=0; i < token.Length; i++)
{
int
count = setOfWords[token[i]];
if
(count > 1)
{
return
token[i];
}
}
return
"NoRepetition"
;
}
static
void
Main() {
string
s =
"Ravi had been saying that he had been there"
;
string
firstWord = findFirstRepeated(s);
if
(firstWord !=
"NoRepetition"
)
Console.WriteLine(
"First repeated word :: "
+ firstWord);
else
Console.WriteLine(
"No Repitition"
);
}
}
Javascript
class GFG
{
static findFirstRepeated(s)
{
var
token = s.split(
" "
);
var
setOfWords =
new
Map();
for
(let i=0; i < token.length; i++)
{
if
(setOfWords.has(token[i]))
{
setOfWords.set(token[i],setOfWords.get(token[i]) + 1);
}
else
{
setOfWords.set(token[i],1);
}
}
for
(let i=0; i < token.length; i++)
{
var
count = setOfWords.get(token[i]);
if
(count > 1)
{
return
token[i];
}
}
return
"NoRepetition"
;
}
static main(args)
{
var
s =
"Ravi had been saying that he had been there"
;
var
firstWord = GFG.findFirstRepeated(s);
if
(firstWord !==
"NoRepetition"
)
{
console.log(
"First repeated word :: "
+ firstWord);
}
else
{
console.log(
"No Repetitionn"
);
}
}
}
GFG.main([]);
Output
First repeated word :: had
Method #2: Using built in python functions:
- As all the words in a sentence are separated by spaces.
- We have to split the sentence by spaces using split().
- We split all the words by spaces and store them in a list.
- Use Counter function to count frequency of words
- Traverse the list and check if any word has frequency greater than 1
- If it is present then print the word and break the loop
Implementation::
Java
import
java.util.*;
class
GFG
{
public
static
String firstRepeatedWord(String sentence)
{
String[] lis = sentence.split(
" "
);
Map<String, Integer> frequency =
new
HashMap<>();
for
(
int
i =
0
; i < lis.length; i++)
{
String word = lis[i];
if
(!frequency.containsKey(word))
{
frequency.put(word,
1
);
}
else
{
frequency.put(word, frequency.get(word) +
1
);
}
}
for
(
int
i =
0
; i < lis.length; i++)
{
String word = lis[i];
if
(frequency.get(word) >
1
)
{
return
word;
}
}
return
"No repeated word found"
;
}
public
static
void
main(String[] args)
{
String sentence =
"Vikram had been saying that he had been there"
;
System.out.println(firstRepeatedWord(sentence));
}
}
Python3
from
collections
import
Counter
def
firstRepeatedWord(sentence):
lis
=
list
(sentence.split(
" "
))
frequency
=
Counter(lis)
for
i
in
lis:
if
(frequency[i] >
1
):
return
i
sentence
=
"Vikram had been saying that he had been there"
print
(firstRepeatedWord(sentence))
Javascript
function
firstRepeatedWord(sentence) {
let lis = sentence.split(
" "
);
let frequency = {};
for
(let i = 0; i < lis.length; i++) {
let word = lis[i];
if
(!frequency[word]) {
frequency[word] = 1;
}
else
{
frequency[word]++;
}
}
for
(let i = 0; i < lis.length; i++) {
let word = lis[i];
if
(frequency[word] > 1) {
return
word;
}
}
}
let sentence =
"Vikram had been saying that he had been there"
;
console.log(firstRepeatedWord(sentence));
Another Approach:
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:
C++
#include <bits/stdc++.h>
using
namespace
std;
void
solve(string s)
{
int
n = s.size();
unordered_map<string,
int
>
mp;
string ans =
""
, t =
""
;
int
min_idx = INT_MAX;
int
i = 0,
j = 0;
while
(j <= n) {
if
(s[j] ==
' '
|| j == n) {
if
(mp[t] == 0) {
mp[t] = i + 1;
}
else
{
if
(min_idx > mp[t]) {
min_idx = mp[t];
ans = t;
}
}
t =
""
;
i = j + 1;
j = i;
}
else
{
t += s[j];
j++;
}
}
if
(ans ==
""
)
cout <<
"No Repetition"
<< endl;
else
cout << ans << endl;
}
int
main()
{
string s1
=
"Ravi had been saying that he had been there"
;
string s2 =
"Ravi had been saying that"
;
string s3 =
"he had had he"
;
solve(s1);
solve(s2);
solve(s3);
return
0;
}
Javascript
function
solve(s)
{
const n = s.length;
const mp =
new
Map();
let ans =
""
, t =
""
;
let min_idx = Number.MAX_SAFE_INTEGER;
let i = 0, j = 0;
while
(j <= n)
{
if
(s[j] ===
' '
|| j === n) {
if
(!mp.has(t)) {
mp.set(t, i + 1);
}
else
{
if
(min_idx > mp.get(t))
{
min_idx = mp.get(t);
ans = t;
}
}
t =
""
;
i = j + 1;
j = i;
}
else
{
t += s[j];
j++;
}
}
if
(ans ===
""
)
console.log(
"No Repetition"
);
else
console.log(ans);
}
const s1 =
"Ravi had been saying that he had been there"
;
const s2 =
"Ravi had been saying that"
;
const s3 =
"he had had he"
;
solve(s1);
solve(s2);
solve(s3);
Java
import
java.io.*;
import
java.util.*;
import
java.util.*;
public
class
Main {
public
static
void
solve(String s) {
int
n = s.length();
Map<String, Integer> mp =
new
HashMap<>();
String ans =
""
, t =
""
;
int
min_idx = Integer.MAX_VALUE;
int
i =
0
, j =
0
;
while
(j <= n) {
if
(j == n || s.charAt(j) ==
' '
) {
if
(mp.getOrDefault(t,
0
) ==
0
) {
mp.put(t, i +
1
);
}
else
{
if
(min_idx > mp.get(t)) {
min_idx = mp.get(t);
ans = t;
}
}
t =
""
;
i = j +
1
;
j = i;
}
else
{
t += s.charAt(j);
j++;
}
}
if
(ans.equals(
""
))
System.out.println(
"No Repetition"
);
else
System.out.println(ans);
}
public
static
void
main(String[] args) {
String s1 =
"Ravi had been saying that he had been there"
;
String s2 =
"Ravi had been saying that"
;
String s3 =
"he had had he"
;
solve(s1);
solve(s2);
solve(s3);
}
}
Output
had No Repetition he
Optimized Approach:
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:
- Create a default dictionary with an initial value of 0, to keep track count of words.
- Iterate through each word in a sentence and increment the count of that word by 1.
- If (count of the word) > 1, return the word.
- If the count of none of the words is greater than 1 then that is we are outside our loop then return “No word is being repeated”.
Implementation:
C++
#include <iostream>
#include <map>
#include <string>
#include <sstream>
using
namespace
std;
string first_repeating_word(string s) {
map<string,
int
> word_count;
stringstream ss(s);
string word;
while
(ss >> word) {
if
(word_count.find(word) != word_count.end()) {
word_count[word]++;
}
else
{
word_count[word] = 1;
}
if
(word_count[word] > 1) {
return
word;
}
}
return
"No word is being repeated"
;
}
int
main() {
string s =
"Ravi had been saying that he had been there"
;
cout << first_repeating_word(s);
return
0;
}
Python3
from
collections
import
defaultdict
def
first_repeating_word(s):
word_count
=
defaultdict(
lambda
:
0
)
for
i
in
s.split():
word_count[i]
+
=
1
if
word_count[i] >
1
:
return
i
return
'No word is being repeated'
if
__name__
=
=
'__main__'
:
s
=
"Ravi had been saying that he had been there"
print
(first_repeating_word(s))
Javascript
<script>
function
first_repeating_word(s){
let word_count =
new
Map()
for
(let i of s.split(
' '
)){
if
(word_count.has(i)){
word_count.set(i,word_count.get(i) + 1);
}
else
word_count.set(i,1);
if
(word_count.get(i) > 1)
return
i
}
return
'No word is being repeated'
}
let s =
"Ravi had been saying that he had been there"
document.write(first_repeating_word(s))
</script>
Time complexity: O(M)
Space 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:
C++
#include <bits/stdc++.h>
using
namespace
std;
string findFirstRepeated(string s)
{
istringstream iss(s);
string token;
set<string> setOfWords;
while
(getline(iss, token,
' '
)) {
if
(setOfWords.find(token) != setOfWords.end()) {
return
token;
}
setOfWords.insert(token);
}
return
"NoRepetition"
;
}
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
import
java.util.*;
public
class
GFG{
static
String findFirstRepeated(String s)
{
String token[] = s.split(
" "
);
HashSet<String> set =
new
HashSet<String>();
for
(
int
i=
0
; i<token.length; i++){
if
(set.contains(token[i])){
return
token[i];
}
set.add(token[i]);
}
return
"NoRepetition"
;
}
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"
);
}
}
Javascript
class GFG
{
static findFirstRepeated(s)
{
var
token = s.split(
" "
);
var
set =
new
Set();
for
(let i=0; i < token.length; i++)
{
if
(set.has(token[i]))
{
return
token[i];
}
set.add(token[i]);
}
return
"NoRepetition"
;
}
static main(args)
{
var
s =
"Ravi had been saying that he had been there"
;
var
firstWord = GFG.findFirstRepeated(s);
if
(firstWord !==
"NoRepetition"
)
{
console.log(
"First repeated word :: "
+ firstWord);
}
else
{
console.log(
"No Repetitionn"
);
}
}
}
GFG.main([]);
# This code is contributed by mukulsomukesh
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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.