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.
classPivote{index:number;pivote:number;isPrimero:boolean;constructor(){this.index=-1;this.pivote=0;this.isPrimero=false;}}constletters:string[]=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];constisZero=(number:number)=>number===0;functionkmp(xArray:number[]):string{// Write your code hereconstpivote=newPivote();xArray.forEach((item:number,index:number)=>{if(!isZero(item)){if(pivote.pivote){if(!(pivote.pivote<item)){if(pivote.pivote!==item){pivote.pivote=item;pivote.isPrimero=false;pivote.index=index;}}}else{pivote.index=index;pivote.isPrimero=index===0?false:true;pivote.pivote=item;}}});letstring:string=letters[pivote.index];if(pivote.isPrimero){constsegundoPiv:Pivote=newPivote();for(leti=pivote.index+1;i<xArray.length;i++){if(!isZero(xArray[i])){segundoPiv.index=i;segundoPiv.pivote=xArray[i];break;}}xArray.forEach((item:number,index:number)=>{if(pivote.pivote===item&&pivote.index===index){for(leti=0;i<segundoPiv.pivote;i++){string+=letters[segundoPiv.index];}for(leti=0;i<pivote.pivote-1;i++){string+=letters[pivote.index];}}elseif(segundoPiv.pivote!==item||segundoPiv.index!==index){for(leti=0;i<item;i++){string+=letters[index];}}});}else{xArray.forEach((item:number,index:number)=>{for(leti=0;i<(pivote.pivote===item&&pivote.index===index?item-1:item);i++){string+=letters[index];}});}returnstring;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Yet Another KMP Problem
You are viewing a single comment's thread. Return to all comments →
TypeScript solution (24/37 passes test cases)