Game of Thrones - I

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