Created
December 16, 2014 07:31
-
-
Save hyunjun/8a7787426b2677e8fd73 to your computer and use it in GitHub Desktop.
[hackerrank] game of throne - I
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX_STR_LEN 100000 | |
#define DICT_LEN 26 | |
char* can_build_palindrome(const char const * s) { | |
int dict[DICT_LEN]; | |
memset(dict, '\0', DICT_LEN); | |
int i = 0; | |
for ( i = 0; i < DICT_LEN; ++i ) { | |
dict[i] = 0; | |
} | |
int len = strlen(s); | |
for ( i = 0; i < len; ++i ) { | |
++dict[*(s + i) - 'a']; | |
} | |
int odd_cnt = 0; | |
for ( i = 0; i < DICT_LEN; ++i ) { | |
if ( dict[i] % 2 == 1 ) { | |
++odd_cnt; | |
} | |
} | |
if ( 0 == odd_cnt || 1 == odd_cnt ) | |
return "YES"; | |
return "NO"; | |
} | |
int main() { | |
char s[MAX_STR_LEN + 1]; | |
memset(s, '\0', MAX_STR_LEN + 1); | |
fscanf(stdin, "%s", s); | |
printf("%s\n", can_build_palindrome(s)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def can_build_palindrome(s): | |
d = {} | |
[d.setdefault(c, []).append(1) for c in s] | |
d = {k: sum(v) for k, v in d.items()} | |
odd_cnt = len([v for v in d.values() if v % 2 == 1]) | |
if odd_cnt in [0, 1]: | |
return 'YES' | |
return 'NO' | |
if __name__ == '__main__': | |
print(can_build_palindrome(raw_input())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def can_build_palindrome(s): | |
d = {} | |
[d.setdefault(c, []).append(1) for c in s] | |
d = {k: sum(v) for k, v in d.items()} | |
odd_cnt = len([v for v in d.values() if v % 2 == 1]) | |
if odd_cnt in [0, 1]: | |
return 'YES' | |
return 'NO' | |
if __name__ == '__main__': | |
print(can_build_palindrome(input())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
public class Solution { | |
public static String canBuildPalindrome(final String s) { | |
Map<Character, Integer> m = new HashMap<Character, Integer>(); | |
int odd_cnt = 0; | |
for ( int i = 0; i < s.length(); ++i ) { | |
char c = s.charAt(i); | |
if ( m.containsKey(c) ) | |
m.put(c, m.get(c) + 1); | |
else | |
m.put(c, 1); | |
} | |
for ( Integer i: m.values() ) { | |
if ( i % 2 == 1 ) | |
++odd_cnt; | |
} | |
if ( 0 == odd_cnt || 1 == odd_cnt ) { | |
return "YES"; | |
} | |
return "NO"; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println(canBuildPalindrome(sc.nextLine())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hackerrank.com/challenges/game-of-thrones | |
import scala.collection.mutable.HashMap | |
object Solution { | |
def canBuildPalindrome(s: String): String = { | |
val m: HashMap[Char, Int] = HashMap() | |
for ( c <- s ) | |
if ( m.contains(c) ) | |
m.put(c, m(c) + 1) | |
else | |
m.put(c, 1) | |
val len: Int = m.retain((_, n) => n % 2 == 1).size | |
if ( 0 == len || 1 == len ) | |
"YES" | |
else | |
"NO" | |
} | |
def main(args: Array[String]) { | |
println(canBuildPalindrome(io.Source.stdin.getLines.next)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment