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.
functiongameOfThrones(key:string):string{// 0. define a map, couting [char] in [key]lethash=newMap<string,number>()for(letcofkey)hash.set(c,(hash.get(c)||0)+1)// 1. define some variableletkey_is_odd:boolean=key.length%2!=0letflag:boolean=true// 2. loop hash and check [char_count] of each [char]...for(constchar_countofhash.values()){constchar_count_is_odd=char_count%2!=0// 2.1 [char_count] that divided by 2, mean it can be split 2 side// -> skip check// 2.2 [char_count] that not divided by 2, but [key] is // -> there is at least one extra char, return NO// 2.3 [char_count] and [key] is both odd, it can be have only 1 extra char// -> if have more than 1, return NO// -> set flag = false to know that already have 1if(!char_count_is_odd)continueif(!key_is_odd)return'NO'if(!flag)return'NO'flag=false}return'YES'}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Thrones - I
You are viewing a single comment's thread. Return to all comments →
My answer with Typescript, noted