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.
Javascript solution with combinatorial analysis with n combining n-2 with n-2 taken, idk if there is a method to turn it more performative, but it was a pretty good algorithm
functionalternate(s){if(s.length===1)return0;letcharSet=[...newSet(s)];letmax=0;letoccSet=[];constisAlternate=(arr)=>{for(leti=0;i<arr.length-1;i++){if(arr[i]===arr[i+1]){returnfalse;}}returntrue;};if(charSet.length===s.length&&isAlternate(s))returns.length;for(letiincharSet){for(letjincharSet){if(i===j)continue;// saving the combinations that already happenedletset=charSet[i]+charSet[j];letreverse=charSet[j]+charSet[i];// If the combination was already analysed, it will pass to the next iterationif(occSet.indexOf(set)>0)continue;// Saving the new combinationoccSet.push(set);occSet.push(reverse);// filterletfiltered=s.split('').filter((v)=>v===charSet[i]||v===charSet[j]).join('');//check if it is max and alternatedif(filtered.length>max&&isAlternate(filtered)){max=filtered.length;}}}returnmax;}
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 →
Javascript solution with combinatorial analysis with n combining n-2 with n-2 taken, idk if there is a method to turn it more performative, but it was a pretty good algorithm