【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.


题解:一个字符串能够通过变换变成一个回文串的充要条件是它里面最多有一种字母,在字符串里面出现的次数是奇数,其他种的字符在字符串里面出现的次数都是偶数。

复制代码
 1 import java.io.*;
 2 import java.util.*;
 3 import java.text.*;
 4 import java.math.*;
 5 import java.util.regex.*;
 6 
 7 public class Solution {
 8 
 9     static String GameOfThronesI(String a){
10         int[] count = new int[26];
11         for(int i =0;i < a.length();i++)
12             count[a.charAt(i)-'a']++;
13         int isOdd = 0;
14         for(int i = 0;i < 26;i++)
15             if(count[i]%2 != 0)
16                 isOdd++;
17         return isOdd <= 1?"YES":"NO";
18     }
19     public static void main(String[] args) {
20         Scanner myScan = new Scanner(System.in);
21         String inputString = myScan.nextLine();
22        
23         // Assign ans a value of s or no, depending on whether or not inputString satisfies the required condition
24         System.out.println(GameOfThronesI(inputString));
25         myScan.close();
26     }
27 }
复制代码

 

posted @   SunshineAtNoon  阅读(361)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示