সমস্যা বিবরণ
একটি স্ট্রিং str দেওয়া আছে (স্পেস-সেপারেটেড ওয়ার্ড)। আমাদের কাজ হল স্ট্রিং-এ প্রথম যে ওয়ার্ডটি রিপিট হয়েছে তা বের করা।
উদাহরণ
ইনপুট: "C program are easy to program" আউটপুট: program
সমাধানের পদ্ধতি
আইস্ট্রিংস্ট্রিম দিয়ে ওয়ার্ড আলাদা করে আনঅর্ডার্ড_ম্যাপে কাউন্ট রাখা। দুয়েন্ড পাস: প্রথমে কাউন্ট, দ্বিতীয়ে প্রথম কাউন্ট>১ वाला ওয়ার্ড রিটার্ন।
প্রোগ্রাম উদাহরণ
#include <bits/stdc++.h>
using namespace std;
string firstRepeatedWord(string str) {
istringstream iss(str);
string w;
unordered_map<string,int> cnt;
while(getline(iss,w,' ')) cnt[w]++;
istringstream iss2(str);
while(getline(iss2,w,' '))
if(cnt[w]>1) return w;
return "NoRepetition";
}
int main() {
string s="C program are easy to program";
cout << firstRepeatedWord(s); // program
return 0;
}
আউটপুট
program