【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 ...
随机推荐
- Vim使用技巧(2) -- 插入模式技巧 【持续更新】
组合快捷键 Ctrl + h //删除前一个字母(同退格键) Ctrl + w //删除前一个单词 Ctrl + u //删除到行首 Esc //切换到普通模式 Ctrl + [ //切换到普通模式 ...
- nginx 用法
nginx配置location总结及rewrite规则写法 如何将 /health 重定向到 /health.html https://stackoverflow.com/questions/4614 ...
- 最新win7系统安全稳定版
最新win7系统32位安全稳定版 V2016年2月,具有更安全.更稳定.更人性化等特点.集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加上萝卜独有人性化的设计.是电脑城.个人.公 ...
- Spark OOM:java heap space,OOM:GC overhead limit exceeded解决方法
问题描述: 在使用spark过程中,有时会因为数据增大,而出现下面两种错误: java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMe ...
- SlidingMenu——使用前的配置
一: 首先下载lib:SlidingMenu.然后将起导入eclipse中,然后将其clean一下,重新生成R文件. 二: 因为SlidingMenu依赖ActionBarSherlock,所以需要下 ...
- Caffe模型读取
caffe模型最终保存使用过的protobuf形式,将一个已经训练好的caffe模型读取出来,可以参考如下: 1,包含的头文件: #include <google/protobuf/io/cod ...
- Laravel5.1 模型 --软删除
软删除是比较实用的一种删除手段,比如说 你有一本账 有一笔记录你觉得不对给删了 过了几天发现不应该删除,这时候软删除的目的就实现了 你可以找到已经被删除的数据进行操作 可以是还原也可以是真正的删除. ...
- Servlet注解
在Servle2.5能够使用注解 在web.xml的<web-app>标签下有一属性metadata-complete="true"在设置true时,Servlet中的 ...
- 45、Android事件总线分发库的使用
事件总线分发库EventBus和Otto的简介及对比 什么是事件总线管理: a.将事件放到队列里,用于管理和分发b.保证应用的各个部分之间高效的通信及数据.事件分发c.模块间解耦 Event Bus是 ...
- p:nth-last-child(2)
<!DOCTYPE html><html><head><style> p:nth-last-child(2){background:#ff0000;}& ...