【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 ...
随机推荐
- Android Notification使用方法
1.http://www.cnblogs.com/plokmju/p/android_Notification.html 2.http://blog.csdn.net/vipzjyno1/articl ...
- 编译包中的 Servlet
编译包中的类与编译其他的类没有什么大的不同.最简单的方法是让您的 java 文件保留完全限定路径,如上面提到的类,将被保留在 com.myorg 中.您还需要在 CLASSPATH 中添加该目录. 假 ...
- nodejs rar/zip加密压缩、解压缩
1.shell/cmd命令行压缩解压缩 (1)zip压缩解压缩 zip压缩:zip -rP{密码} <目标文件.zip> <源文件> //默认覆盖现有文件 zip解压缩:zip ...
- 使用shell脚本守护node进程
现在开源的守护node进程的包有不少,比如forever,pm2,这里我就不再赘述了. 但是有的公司生产服务器是不能联网的,而这些包都需要全局安装,必须要网络环境.难道你nohup node app. ...
- lumen 获得当前uri 如/xxx/{id}
因为想实现通过url判断是否有权限,所有需要拿到当前的route方法的name,如下 $api->get('role/grant/{id}', 'RoleController@getGrant' ...
- 生产者——消费者模型的java代码实现
生产者 import java.util.Random; public class Producer extends Thread { private Storage<Product> s ...
- Python类基础知识(面向对象基础)
#首先 我们需要了解 面向过程是什么 面向对象是什么 我们为什么需要使用面向对象 面向过程:根据业务逻辑从上到下写垒代码 面向对象:根据代码对函数进行分类和封装 区别:解决问题的逻辑不同,但是都能解决 ...
- Docker学习(1安装docker)
1.在安装之前首先查看系统版本,ubuntu centos6 7 安装方式并不相同,所以首先确定系统版本 #cat /etc/issue 确定系统版本 2.开始安装Docker (1)Ubuntu下 ...
- HDU4027(Can you answer these queries?)
题目链接:传送门 题目大意:有一个长度为n的数组,有m次操作,每次操作输入 v x y,v==0时x~y区间内的数都开平方并且向下取整,v==1时求x~y区间内所有数的和. 题目思路:long lon ...
- 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 " 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...