Of course, we can just implement the same algorithm in Javascript, we just need to implement a group function. In this case, I have implemented it as a reducer function so that I can use it in an array method chain.
constlast=arr=>arr[arr.length-1]constappendToLast=(arr,x)=>{constlastElement=last(arr)lastElement.push(x)returnarr}constgroup=(prev,x)=>prev.length===0?[[x]]:last(prev)[0]===x?appendToLast(prev,x):[...prev,[x]]// Everything above this line is because Javascript doesn't have group built in.constlength=arr=>arr.lengthconstisAlmostLucky=groupedFreqs=>groupedFreqs.length===2&&groupedFreqs[1].length===1&&groupedFreqs[1][0]===groupedFreqs[0][0]+1constisLucky=str=>{constgroupedFreqs=str.split('').sort().reduce(group,[]).map(length).sort().reduce(group,[])returngroupedFreqs.length===1||isAlmostLucky(groupedFreqs)}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Of course, we can just implement the same algorithm in Javascript, we just need to implement a
group
function. In this case, I have implemented it as a reducer function so that I can use it in an array method chain.