We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 712dab1 commit fbcc38eCopy full SHA for fbcc38e
HashMaps/first_duplicate_word.py
@@ -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