Saturday, 2 March 2019

Input: 


T number of test-cases, each with n number of strings

Objective:


To find the most repeating string (in dictionary order) for each test case.

Approach:


Using c++ stl, map

Try it here


 #include <bits/stdc++.h>  
 using namespace std;  
 int main()  
 {  
   int t;  
   cin>>t;  
   while(t--){  
        int n; cin>>n;  
        unordered_map<string, int> um;  
        string word;  
        for(int i = 0; i < n; i++){  
          cin>>word;  
          um[word]++;  
        }  
        unordered_map<string, int> :: iterator it;  
        string res; int max = 0;  
        for(auto it : um){  
          if(it.second > max){  
            max = it.second;  
            res = it.first;  
          }  
          else if(it.second == max && it.first < res){  
            max = it.second;  
            res = it.first;  
          }  
        }  
        cout<<res<<endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment