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.
functionalternate(s:string):number{// get a set of character in [s]letchars:string[]=Array.from(newSet<string>(s.split('')))// get everys pair of 2 character in [s]letpairs:[string,string,string?,number?][]=[]for(leti=0;i<chars.length;i++){for(letj=i+1;j<chars.length;j++){pairs.push([chars[i],chars[j]])}}// check each pair, save info after remove other character except character in that pairfor(leti=0;i<pairs.length;i++){letpair=pairs[i]letastr=s.split('').filter(c=>pair.includes(c))pair.push(astr.join(''))pair.push(astr.every((c,j)=>c!=astr[j+1])?astr.length:0)}console.log(pairs)// return the largest valid result in [pairs]returnMath.max(0,...pairs.map(([a,b,str,result])=>result))}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
My answer in Typescipt, simple, not minimized