Hacker Rank Solutions: Game of Thrones

Hacker Rank Solutions: Game of Thrones

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def palindrome?(string)
  hash = {}

  string.each_char do |c|
    if hash[c] == nil
      hash[c] = 1
    else
      hash[c] += 1
    end
  end

  odd_chars = 0

  hash.each do |k,v|
    unless v % 2 == 0
      odd_chars += 1
      if odd_chars >= 2
        puts 'NO'
        return
      end
    end
  end

  puts 'YES'
end

string = $stdin.gets.chomp

palindrome?(string)

Comments