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 ...
随机推荐
- 使用virtualbox 配置 linux host-only虚拟主机连接外网(转载)
host-only 下的虚拟机之间可以互相访问,虚拟机和宿主机可以互相访问,但是虚拟机不能访问外网. 需要设置: 1.宿主机设置 先对宿主机(windows机器,我这里是win7系统)进行相关配置. ...
- 简易扩展Visual Studio UnitTesting支持TestMethodCase
NUnit的TestCaseAttribute可以简化大量的测试参数输入用例的编写,如果基于Visual Studio Unit Test Project开发则默认没有类似的功能,看一段对比代码: p ...
- docker自动开启端口转发功能
yum -y install epel-release yum -y install docker-io service docker start docker pull haproxy # 此时自动 ...
- Unity3D笔记十九 持久化数据
1.PlayerPrefs类(生命周期???) 1.1 保存与读取数据 在C#中类似缓存.Cookie.Session等保存数据的,但是有点区别的是在C#中如果在取值时没有取到默认值则返回值是NULL ...
- VS2015:出现devenv.sln解决方案保存对话框
问题描述: 打开VS2015项目时,提示保存“devenv.sln” 解决方法: 找到文件:C:\Program Files (x86)\Common Files\microsoft shared\M ...
- hdu3038 How many answers are wrong【并查集】
TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always w ...
- poj1269 intersecting lines【计算几何】
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...
- Linux系统下便捷使用中国知网的方式
https://blog.csdn.net/mowangajimide/article/details/54144379
- 解决VMware安装ubuntu16.04后无法全屏的问题
参考教程:http://www.jb51.net/os/Ubuntu/356462.html 双系统经常崩,故在windows10下装了个ubuntu的虚拟机,安装完成后无法全屏,进入系统设置调试显示 ...
- c# 实现ListView的排序
[问题描述]: 当点击列标题的时候,能够完成对该列排序,同时显示排序的箭头,再次点击,按照反序排序. [解决方法]: 1.创建一个类:ListViewColumnSorter继承IComparer接口 ...