ECMAR20F - Editorial

PROBLEM LINK:

Practice Problem
Contest Link

Author: Akash Kumar Bhagat
Tester: Sandeep Singh
Editorialist: Akash Kumar Bhagat

DIFFICULTY:

EASY

PREREQUISITES:

SET, Implementation

PROBLEM:

You are given a string of small letter English alphabets that signifies a fossil. One has to calculate the boredom level of the visitor which increases by 1 if the visitor sees any repetition of the fossil.
In other words, one have to print the difference between spaces and unique fossils displayed.

EXPLANATION:

According to the question, one has to print the difference of the space in the museum with the number of unique fossils. Since the constrain of the fossil is small (a-z), one can use brute force to check whether each element/alphabet is present in the list or not.
One can find the number of unique characters in various ways. Say the number of unique characters/fossil is A and the number of space in the museum be C, the answer ANS will be.

ANS = C - minimum of(C,A)

SOLUTIONS:

Python 3.7
for _ in range(int(input())):
    N,C=map(int,input().split())
    arr=input()
    A=len(set(arr))
    ANS=C-min(C,A)
    print(ANS)
 
C++14
#include <bits/stdc++.h>
#define ll long long int
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define scanarr(a,b,c) for( i=b;i<c;i++)cin>>a[i]
#define showarr(a,b,c) for( i=b;i<c;i++)cout<<a[i]<<' '
#define ln cout<<'\n'
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAX 100005
using namespace std;
////////////////////////////////////////////////////////////////CODE STARTS HERE////////////////////////////////////////////////////////////////
 
void solve(){
    int n,i,j,c;
 
    cin>>n>>c;
    string st;
    cin >> st;
 
    set<char> s;
    for(i=0;i<n;i++){
        s.insert(st[i]);
 	if (s.size() == 26) break;
 	}
    int tmp=s.size();
 
   // cout<<(c-(s.size()))<<endl;
    
    cout<<max(0,c-tmp);
    ln;
 
 
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    int t;
	ios_base::sync_with_stdio(false);cin.tie(NULL);
    cin>>t;
    while(t--)
        solve();
}   
JAVA
import java.util.*;
 
class Solution{
 
    public static void main(String args[]){
        Scanner sc = new Scanner (System.in);
        
        int test,N,C;
 
        test = sc.nextInt();
 
        while(test-->0){
 
            N = sc.nextInt();
            C = sc.nextInt();
 
            String s = sc.next();
 
            Set<Character> set = new HashSet<>();
 
            for(int  i = 0; i<N; i++){
                set.add(s.charAt(i));
                // if (set.size() == 26) break;
            }
            // System.out.println(set.size());
            System.out.println(C - Math.min(C,set.size()));
 
        }
 
        sc.close();
    }
 
} 
3 Likes