Sunday, December 3, 2017

N Strings - Largest Unique Characters

N string values are passed as input to the program. The output is the string which has the largest count of unique characters. If multiple strings have the same largest count of unique characters, print the last occurring string.

Input Format:
The first line contains N. Next N lines contain the N string values.

Output Format:
The first line contains the string value which has the largest count of unique characters.

Boundary Conditions: 2 <= N <= 1000 1 <= Length of a string value S(i) <= 10^5

Example Input/Output 1:
Input:
3
aaaaaaaaaa abcdwjdkjwjddcbakd abcdefghi
Output:
abcdefghi

Solution 1:
n,a,b,c=int(input()),[],[],0
for i in range(n):
   b.append(input().strip())
   a.append(len(set(b[i])))
for i in range(len(a)):
  if a[i]>=c:
     c,x=a[i],b[i]
print(''.join(i for i in x))

Solution 2:
#include <iostream>
#include <map>
using namespace std;
int main()
{
  int n,x=0;
  string s,p;
  cin>>n;
  while(n--)
  {
      int i,c=0;
      map<char,int> m;
      cin>>s;
      for(int i=0;i<s.size();i++)
        if(++m[s[i]]==1)
            c+=1;
      if(c>=x) x=c,p=s;
  }
  cout<<p;
}
Share: