Skip to content

Commit fbcc38e

Browse files
author
ybenchekroun
authored
Create first_duplicate_word.py
1 parent 712dab1 commit fbcc38e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

HashMaps/first_duplicate_word.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Problem Statement : Given a string find the first duplicate word,
2+
example string: “this is just a wonder, wonder why do I have this in mind
3+
4+
5+
6+
- The right data-structure to check for existence :
7+
set in Python (order in not preserved)
8+
9+
10+
```
11+
string = "this is just a wonder, wonder why do I have this in mind"
12+
def firstduplicate(string: str) -> str:
13+
import re
14+
cleanStr = re.sub("[^a-zA-Z -]", "", string)
15+
16+
words = cleanStr.lower().split(" ")
17+
seen_words = set()
18+
for word in words:
19+
if word in seen_words:
20+
return word
21+
else:
22+
seen_words.add(word)
23+
return None
24+
firstduplicate(string)
25+
26+
```

0 commit comments

Comments
 (0)