একটি স্ট্রিং দেওয়া হয়েছে .আমাদের কাজ হল প্রদত্ত স্ট্রিং-এ প্রথম বার বার শব্দ খুঁজে বের করা৷ এই সমস্যাটি বাস্তবায়নের জন্য আমরা পাইথন সংগ্রহগুলি ব্যবহার করছি৷ সংগ্রহ থেকে, আমরা কাউন্টার() পদ্ধতি পেতে পারি।
অ্যালগরিদম
Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1
উদাহরণ কোড
# To Find the first repeated word in a string from collections
import Counter
def repeatedword(n):
# first split given string separated by space into words
w = n.split(' ')
con = Counter(w)
for key in w:
if con[key]>1:
print ("REPEATED WORD IS ::>",key)
return
# Driver program
if __name__ == "__main__":
n=input("Enter the String ::")
repeatedword(n)
আউটপুট
Enter the String ::We are all peaceful soul and blissful soul and loveful soul happy soul REPEATED WORD IS ::> soul