【HackerRank】 Game Of Thrones - I
King Robert has 7 kingdoms under his rule. He gets to know from a raven that the Dothraki are going to wage a war against him soon. But, he knows the Dothraki need to cross the narrow river to enter his dynasty. There is only one bridge that connects both sides of the river which is sealed by a huge door.
The king wants to lock the door, so that, the Dothraki can't enter. But, to lock the door he needs a key that is an anagram of a certain palindrome string.
The king has a list of words. Help him figure out if any anagram of the words can be a palindrome or not?
Input Format
A single line which contains the input string
Constraints
1<=length of string <= 10^5
Each character of the string is a lowercase english letter.
Output Format
A single line which contains YES/NO in capital letter of english alphabet.
题解:一个字符串能够通过变换变成一个回文串的充要条件是它里面最多有一种字母,在字符串里面出现的次数是奇数,其他种的字符在字符串里面出现的次数都是偶数。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution { static String GameOfThronesI(String a){
int[] count = new int[26];
for(int i =0;i < a.length();i++)
count[a.charAt(i)-'a']++;
int isOdd = 0;
for(int i = 0;i < 26;i++)
if(count[i]%2 != 0)
isOdd++;
return isOdd <= 1?"YES":"NO";
}
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
String inputString = myScan.nextLine(); // Assign ans a value of s or no, depending on whether or not inputString satisfies the required condition
System.out.println(GameOfThronesI(inputString));
myScan.close();
}
}
【HackerRank】 Game Of Thrones - I的更多相关文章
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
随机推荐
- LeetCode560. Subarray Sum Equals K
Description Given an array of integers and an integer k, you need to find the total number of contin ...
- C++之类的静态成员变量和静态成员函数
static静态成员函数 在类中.static 除了声明静态成员变量,还能够声明静态成员函数. 普通成员函数能够訪问全部成员变量.而静态成员函数仅仅能訪问静态成员变量. 我们知道.当调用一个对象的成员 ...
- php 远程调用redis
<?php $redis_conf = array ( "active_code"=>array( "host" => "14.29 ...
- Linux Linux程序练习一
注意:在Linux中,"*"是一个通配符,代表所有字符,所以"*"必须要使用转义字符"\"
- 游戏开发之coco2dx ---简化提炼tolua
http://www.cnblogs.com/gl5773477/p/4234613.html
- 创建一个Material Design应用过程
创建一个使用Material主题的应用 1.这里需要先搭建一个应用的运行环境 创建一个AVD: 然后运行这个AVD. 2.创建应用 其中的Min SDK和Target SDK 都选择了L Preive ...
- zmq重点
The zmq_msg_send(3) method does not actually send the message to the socket connection(s). It queues ...
- 41、Android中当数据库需要更新时我们该怎么办?
转载 http://blog.csdn.net/jiangwei0910410003/article/details/39670813
- Young Maids
E - Young Maids Time limit : 2sec / Memory limit : 256MB Score : 800 points Problem Statement Let N ...
- 【转】Mysql之binlog日志说明及利用binlog日志恢复数据操作记录
众所周知,binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷! 废 ...