Game of Thrones - I

  • + 0 comments

    Javascript:

    function gameOfThrones(s) {
    		//strMap stores each letter and its frequency
        const strMap = new Map([]);
    		
    		//oddSet stores the alphabet that appears an odd number of times in the string
        const oddSet = new Set([]);
        
        for (let i = 0; i < s.length; i++) {
    				//add every unique alphabet in the strMap along with the number of times it appears in the string.
            strMap.set(s[i], (strMap.get(s[i]) ?? 0) + 1);
    
            if (strMap.get(s[i]) % 2 !== 0) {
    				//if the frequency of the alphabet modded with 2 is not zero, it is odd and must be added to the oddSet
                oddSet.add(s[i])
            } else {
    						//if its occurence is divisible by 2, it is even and we remove it from the oddSet
                oddSet.delete(s[i])
            }
        }
    
    if (oddSet.size > 1) return "NO"
    return "YES"
    

    } `