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.
functionanagram(s:string):number{// 0. check s length is odd, so it cann't be split, return -1if(s.length%2!=0)return-1// 1. split [s] into left part [l] and right part [r], define [m]&[o] to countletl=s.substring(0,s.length/2)letr=s.substring(s.length/2)letm=newMap<string,number>()leto=0// 2. count [l] into [m], check [r], count++for(letcofl)m.set(c,(m.get(c)||0)+1)for(letcofr){if(m.has(c)&&m.get(c)>0)m.set(c,m.get(c)-1)elseo++}// 3. return [o]returno}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Anagram
You are viewing a single comment's thread. Return to all comments →
My answer with Typescrip