Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,973 views
in Online Assessments by Expert (108,110 points) | 1,973 views

2 Answers

0 like 0 dislike
Best answer
The School of Langauge and Science teaches five subjects: Physics, Chemistry, Math, Botany, and Zoology. each student is skilled in one subject. The skills of the students are desccribed by string of a=named skills that consists of the letters p,c,m,b, abd z only. Each character describes the skill of a student.

Given a list of students' skills, determine the total number of different teams satisfying the following constrains:

A team consists of a group of exactly five students.
Each student is skilled in a different subject.
A student may only be on one team.
Example 1
skills = pcmbzpcmbz

There are 2 possible teams that can be formed at one time: skills[0-4] = pcmbz and skills[5-9] = pcmbz, for example.

Example 2
skills = mppzombpzcmpmzz

The sorted string is bbbbcccmmmmppppzzzz. All of the skills are represented, but there are only 3 students skilled in Chemistry. Only 3 teams can be created.

Function Description
Complete the function differentTeams in the editor below. The function must return an integer value representing the number of teams that can be formed given the constraints. differentTeams has the following parameter(s): string skills: a string of length n where each position represents the Skill of a student.

My Solution:

function perfectTeam(skills) {
    
    let unique = new Set(skills);
    
    if([...unique].sort().join('') !== 'bcmpz') {
        return 0;
    }
    
    let counter = {};
    
    [...skills].forEach(v => {
        counter[v] ? counter[v]++ : counter[v] = 1;
    });
    
    return Object.values(counter).sort((a,b) => a-b)[0];  
}
by Expert (108,110 points)
0 like 0 dislike

Simple o(N) solution is here we don't have to use set and sorting. And don't need to sort object values also just maintain a variable min.

 

function getMinGroup(str){
let mapSubjects={};
let keysCount=0;
const len=str.length;

//get the count in map for each skill
for(let i=0;i<len;i++){
const subject=str[i];
if(subject==='b' || subject==='m' || subject==='c' || subject==='p' || subject==='z'){
if(!mapSubjects[subject]){
keysCount++;
mapSubjects[subject]=1;
}else{
mapSubjects[subject]=mapSubjects[subject]+1;
}

}
}

//Subject count is 5 we know already
if(keysCount!==5){
return 0;
}

let min=Infinity;

//itrate over the object keys and values to get min
for(let [key,val] of Object.entries(mapSubjects)){
if(val<min){
min=val;
}
}

 

return min

 

}

by Expert (108,110 points)