Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Podmienky pre zistenie palindrómu:
- ak je dĺžka reťazca párna, potom sa musí každý znak vyskytovať v reťazci párny počet krát
- ak je dĺžka reťazca nepárne číslo, potom musí byť výskyt iba jedného znaku nepárny počet krát, ostatné sa musia vyskytovať párny počet krát
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
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 |