We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is in JS. Hope you guys find it easy to get, also hope to know any improvement I can make.
functionsteadyGene(gene:string):number{// Write your code hereconstn=gene.lengthconstexRep=n/4// expected repetitionconstrep:Record<string,number>={A:0,T:0,G:0,C:0}// repetition mapconstoverRep:Record<string,number>={A:0,T:0,G:0,C:0}// over-repeatedconstpos:[string,number][]=[]// over-repeated position// check repetitionfor(leti=0;i<=n-1;i++){rep[gene[i]]+=1}overRep.A=Math.max(rep.A-exRep,0)overRep.T=Math.max(rep.T-exRep,0)overRep.G=Math.max(rep.G-exRep,0)overRep.C=Math.max(rep.C-exRep,0)if(overRep.A===0&&overRep.T===0&&overRep.G===0&&overRep.C===0)return0// Now it's broken down to finding the smallest substring that contains the minimum counts of the letters.// Filter over-repeatedfor(letj=0;j<=n-1;j++){if(overRep[gene[j]]>0)pos.push([gene[j],j])}letminLength;lettempStart:number=0;constletterCount:Record<string,number>={A:0,T:0,G:0,C:0}consthasAllLetters=()=>letterCount.A>=overRep.A&&letterCount.T>=overRep.T&&letterCount.G>=overRep.G&&letterCount.C>=overRep.Cconsole.log({pos,overRep})for(letk=0;k<=pos.length-1;k++){const[letter,index]=pos[k];letterCount[letter]=letterCount[letter]+1while(hasAllLetters()){const[startLetter,startIndex]=pos[tempStart]constnewLength=index-startIndex+1if(minLength===undefined||newLength<minLength)minLength=newLengthletterCount[startLetter]=letterCount[startLetter]-1tempStart=tempStart+1}}returnminLength}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →
This is in JS. Hope you guys find it easy to get, also hope to know any improvement I can make.