858D. Polycarp's phone book - Codeforces Solution C++

  Problem Link : 858D. Polycarp's phone book 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	string arr[n];
	unordered_map<string,int> mp;

	string s;
	for(int i=0;i<n;i++)
	{	
		cin>>s;
		arr[i]=s;
		unordered_set<string> us;
		for(int i=0;i<s.length();i++)
		{
			string p="";
			for(int j=i;j<s.length();j++)
			{
				p+=s[j];
				if(us.find(p)==us.end())
				{
					us.insert(p);
					mp[p]++;
				}

			}
		}
	}
	string t="";
	for(int i=0;i<n;i++)
	{
		s=arr[i];
		bool flag=0;
		for(int j=1;j<=9;j++)
		{
			for(int k=0;k<=9-j;k++)
			{
				t=s.substr(k,j);
				if(mp[t]<=1)
				{
					flag=1;
					cout<<t<<"\n";
					break;
				}
			}

			if(flag)
				break;
		}
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!