Game of Thrones - I

Sort by

recency

|

1080 Discussions

|

  • + 0 comments
    from collections import Counter
    def gameOfThrones(s):
        freq=Counter(s)
        c=0
        for i in freq:
            if freq[i]%2==1:
                c+=1
        return 'NO' if c>1 else 'YES'
    
  • + 0 comments

    My answer with Typescript, noted

    function gameOfThrones(key: string): string {
        // 0. define a map, couting [char] in [key]
        let hash = new Map<string, number>()
        for (let c of key) hash.set(c, (hash.get(c) || 0) + 1)
        
        // 1. define some variable
        let key_is_odd: boolean = key.length % 2 != 0
        let flag: boolean = true
    
        // 2. loop hash and check [char_count] of each [char]...
        for (const char_count of hash.values()) {
            const char_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 1
            if (!char_count_is_odd) continue
            if (!key_is_odd) return 'NO'
            if (!flag) return 'NO'
    
            flag = false
        }
    
        return 'YES'
    }
    
  • + 0 comments

    Perl:

    sub gameOfThrones {
        my @s = split("", shift);
        my %h;
        my $cnt = 0;
    
        foreach (@s) {
            $h{$_}++;
        }
    
        foreach my $v (values %h) {
            if ($v % 2 > 0) {
                $cnt++;
            }        
        }  
    
        return ($cnt == 1 || $cnt == 0) ? "YES" : "NO";
    }
    
  • + 0 comments

    The odd even check is actually unnecessary I've just figured. You can skip that part but the logic is pretty much the same. Java:

    public static String gameOfThrones(String s) {
      int n = s.length();
      if (n == 0) {
        return "NO";
      } else if (n == 1) {
        return "YES";
      }
      int[] alphabet = new int[26];
    
      for (char c : s.toCharArray()) {
        alphabet[c - 'a']++;
      }
      if (n % 2 == 0) {
        for (int freq : alphabet) {
          if (freq % 2 != 0) {
            return "NO";
          }
        }
        return "YES";
      } else {
        int oddCount = 0;
        for (int freq : alphabet) {
          if (freq % 2 != 0) {
            oddCount++;
          }
        }
        if (oddCount == 1) {
          return "YES";
        } else {
          return "NO";
        }
      }
    }
    }
    
  • + 0 comments

    Here is my c++ solution : explanation here : https://youtu.be/yhSaL48IHds

    solution 1 :

    string gameOfThrones(string s) {
        map<char, int> mp;
        for(int i = 0; i < s.size(); i++){
            if(mp[s[i]] != 0){
                mp[s[i]]--;
                if(mp[s[i]] == 0) mp.erase(s[i]);
            }
            else mp[s[i]]++;
        }
        if(mp.size() > 1) return "NO";
        return "YES";
    }
    

    solution 2 :

    string gameOfThrones(string s) {
        map<char, int> mp;
        for(int i = 0; i < s.size(); i++){
            if(mp[s[i]] == 1)  mp.erase(s[i]);
            else mp[s[i]]++;
        }
        if(mp.size() > 1) return "NO";
        return "YES";
    }