【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 ...
随机推荐
- hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。
A Simple Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- JPA(Java Persistence API)Java持久化API-介绍
JPA全称: Java Persistence API JPA的宗旨是为POJO提供持久化标准规范,能够脱离容器独立运行,很方便开发和测试.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关 ...
- python 使用正则表达式的爬虫
下面我们一起尝试一下爬取内涵段子网站: http://www.neihan8.com/article/list_5_1.html 打开之后,不难看到里面一个一个灰常有内涵的段子,当你进行翻页的时候,注 ...
- Android-ViewPagerIndicator框架使用——LinePageIndicator
前言:LinePageIndicator类似CirclePageIndicator,只是将圆点指示变成了长条指示. 一:使用是定义的布局文件simple_lines : <LinearLayou ...
- C++中的return返回值:return0 or return -1?
C++98 中定义了如下两种 main 函数的定义方式: int main( ) int main( int argc, char *argv[] ) (参考资料:ISO/IEC 14882(19 ...
- Laravel5.1 模型 --一对一关系
这篇文章主要记录模型的一对一关系,关联关系是Model的一种非常方便的功能. 1 实现一对一关系 1.1 准备工作 首先我们需要创建两张表和对应的两个模型,第一个模型是用户表,第二个模型是账号表. 这 ...
- HashMap的clear()方法和new HashMap的效率问题
最近研究Lucene的时候,遇到的用到大量Map的问题,心生好奇,想看一下在1W,10W,100W三种数据量下,new HashMap ,与 HashMap.clear()方法的效率问题. 提前说明: ...
- Android无线测试之—UiAutomator UiSelector API介绍之四
四种匹配关系介绍 一.四种匹配关系介绍: 二.举例: 匹配字符串 0123456789 1.完全匹配: 0123456789 2.包含匹配: 45678.456.678 3.正则匹配: \d{10 ...
- &#x编码转换成汉字
import java.io.UnsupportedEncodingException; public class UnicodeDecoder { public static String esca ...
- android学习三---创建第一个程序
1.创建一个Helloworld程序 1.1 new-android application 点击file-new-android application出现如下界面 填上应用名,项目名,包名,选择所 ...