Game of Thrones - I

Sort by

recency

|

1077 Discussions

|

  • + 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";
    }
    
  • + 0 comments

    Here it is Java Code:

    import java.util.*;

    class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); System.out.println(got(s));

    }
    public static String got(String s){
         char[] ar = s.toCharArray();
        Arrays.sort(ar);
    
        int i =0;
        int errorcount=0;
        while(i<ar.length){
            if(i<ar.length-1 && ar[i]==ar[i+1]){
                i=i+2;
            }
            else{
                i++;
                errorcount++;
            }
        }
            if(errorcount <= 1){
                return "YES";
    
            }
            else{
                return "NO";
            }
    
    }
    

    }

  • + 0 comments

    https://github.com/Achintha444/problem-solving-hackerrank-js/blob/main/98-game-of-thrones.js

    Answer in javascript

  • + 0 comments

    One liner.

    def gameOfThrones(s):

    return "NO" if sum([1 for i in [s.count(i) for i in set(s)] if i % 2 != 0]) > 1 else "YES"