Twitter OA prepare: Anagram is A Palindrome

Algorithm:
Count the number of occurrence of each character.
Only one character with odd occurrence is allowed since in a palindrome maximum number of character with odd occurrence can be '1'.
All other character should occur in even number of times.
If (2) and (3) fail, then the given string is not a palindrome.
public int check(String str) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if (map.containsKey(c)) {
map.put(c, map.get(c)+1);
}
else {
map.put(c, 1);
}
}
int cntOdd = 0;
for (int val : map.values()) {
if (val % 2 == 1) {
cntOdd++;
}
}
return cntOdd>1? 0 : 1;
}
Twitter OA prepare: Anagram is A Palindrome的更多相关文章
- Twitter OA prepare: Two Operations
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...
- Twitter OA prepare: even sum pairs
思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2
- Twitter OA prepare: K-complementary pair
2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...
- Twitter OA prepare: Visit element of the array
分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- Twitter OA prepare: Flipping a bit
You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...
- Twitter OA prepare: Equilibrium index of an array
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
- 2Sigma OA prepare: Friends Circle
DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...
随机推荐
- 题目1100:最短路径(最短路径问题进阶dijkstra算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1100 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 题目1447:最短路(Floyd算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1447 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- Artech的MVC4框架学习——第七章Action的执行
概况:Action的执行不仅包含action方法的执行,还包含相关筛选器的执行. 第一基于线程池的请求,http请求称谓工作线程(p321),基于线程池优势:工作线程重用和工作线程数量的限制. 第二两 ...
- 如何搭建Packetbeat性能监控
安装与配置JDK 1. 将jdk-8u111-linux-x64.tar.gz上传至Linux的/opt目录下,并执行解压命令: tar -zxvf jdk-8u111-linux-x64.tar. ...
- [HTML5]移动平台的HTML5开发框架
jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...
- Save vtkMatrix4x4 to a file 保存到文件
vtkMatrix4x4是VTK中的一个表示4x4矩阵的一种数据结构,有时候我们想把其保存到一个文件中,那么可以使用如下的代码: void writeVtkMatrix4x4ToFile(const ...
- 为什么 Redis 重启后没有正确恢复之前的内存数据
安装 Redis 后,默认配置下启动会得到如下日志: [] Sep ::! Background save may fail under low memory condition. To fix th ...
- 关于jquery的css的一些知识
Query实例CSS 样式表动态选择本实例主要说的还是jquery的选择器,关于jquery的css的一些知识用类似 $("li").css("cursor", ...
- 老师的blog整理
python基础部分: 宝哥blog: https://www.cnblogs.com/guobaoyuan/ 开哥blog: https://home.cnblogs.com/u/Neeo 女神笔记 ...
- MTU-TCP/IP协议栈-linux kernel-TCP丢包重传-UDP高性能-AI- ip数据报 tcp数据报
1.IP协议首部 TCP报文段的首部 UDP分组结构 ip数据报 tcp数据报 UDP校验 w 报文长度该字段指定UDP报头和数据总共占用的长度.可能的最小长度是8字节,因为UDP报头已经占用了 ...