Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Palindrome is when:
- if the length of a string is even, then each character needs to be even times in the string
- if the length of a string is odd, then there is only one character, which count in the string is odd
I created solution in:
All solutions are also available on my GitHub.
Java
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.*; public class GameOfThrones1 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String line = stdin.nextLine(); Map<Character, Integer> charMap = new HashMap<>(); for(int i = 0; i < line.length(); i++) { char c = line.charAt(i); int count = charMap.getOrDefault(c, 0); charMap.put(c, count + 1); } System.out.println(isPalindrome(line.length(), charMap)); stdin.close(); } private static String isPalindrome(int length, Map<Character, Integer> charMap) { boolean isPalindrome = true; if(length % 2 == 0) { for(char key : charMap.keySet()) { if(charMap.get(key) % 2 != 0) { isPalindrome = false; break; } } } if(length % 2 != 0) { int oddCount = 0; for(char key : charMap.keySet()) { if(charMap.get(key) % 2 != 0) { oddCount++; } } isPalindrome = oddCount == 1; } if(isPalindrome) { return "YES"; } else { return "NO"; } } } |
JavaScript
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
'use strict'; let isPalindrome = (length, charMap) => { let isPalindrome = true; if(length % 2 == 0) { for(let prop in charMap) { if(charMap.hasOwnProperty(prop) && charMap[prop] % 2 !== 0) { isPalindrome = false; break; } } } if(length % 2 != 0) { let oddCount = 0; for(let prop in charMap) { if(charMap[prop] % 2 !== 0) { oddCount++; } } isPalindrome = oddCount === 1; } if(isPalindrome) { return "YES"; } else { return "NO"; } }; const processData = input => { let charMap = {}; for(let i = 0; i < input.length; i++) { let count = charMap[input[i]]; if(count) { charMap[input[i]] = count + 1; } else { charMap[input[i]] = 1; } } console.log(isPalindrome(input.length, charMap)); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import scala.io.Source object GameOfThrones1 extends App { val line = Source.stdin.bufferedReader().readLine() validate(line) def isPalindrome(length: Int, charMap: Map[Char, String]): Boolean = { if(length % 2 == 0) charMap.forall(entry => entry._2.length % 2 == 0) else charMap.count(entry => entry._2.length % 2 == 1) == 1 } def validate(line: String): Unit = { val charMap = line.groupBy(c => c) println(if(isPalindrome(line.length, charMap)) "YES" else "NO") } } |
Ruby
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 |
def can_be_palindrome(input) char_hash = Hash.new(0) input.each_char { |c| char_hash[c] += 1 } if input.length.even? char_hash.each_key { |key| return 0 if char_hash[key].odd? } else has_odd = false char_hash.each_key do |key| return 0 if has_odd && char_hash[key].odd? has_odd = true if char_hash[key].odd? end end return 1 end string = gets.chomp found = can_be_palindrome(string) if found == 1 puts "YES" else puts "NO" end |