Algorithm:

  1. Count the number of occurrence of each character.

  2. Only one character with odd occurrence is allowed since in a palindrome maximum number of character with odd occurrence can be '1'.

  3. All other character should occur in even number of times.

  4. 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的更多相关文章

  1. Twitter OA prepare: Two Operations

    准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...

  2. Twitter OA prepare: even sum pairs

    思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2

  3. Twitter OA prepare: K-complementary pair

    2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...

  4. Twitter OA prepare: Visit element of the array

    分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...

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

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

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

  8. 2Sigma OA prepare: Longest Chain

    DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...

  9. 2Sigma OA prepare: Friends Circle

    DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...

随机推荐

  1. 题目1099:后缀子串排序(qsort函数自定义cmp函数)

    题目链接:http://ac.jobdu.com/problem.php?pid=1099 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. php应用

    1. php判断是否为数字 is_numeric() 这个函数就是检测参数是否为数字,如果是就返回true,如果不是就返回false is_numeric( 'abcd123' ) or die('提 ...

  3. 搭建IPv4专有网络

    搭建IPv4专有网络 版权归属:阿里云网站   本教程将指引您搭建一个具有IPv4地址块的专有网络,并为专有网络中的ECS实例绑定一个弹性公网IP(EIP)进行公网访问. 步骤一:创建专有网络和交换机 ...

  4. SDRAM相位角计算

    SDRAM相位角计算 下面是我复制别人的没有图片 如果想看原文 点击下面链接,, http://wenku.baidu.com/view/91e2d76a27284b73f24250e6.html 一 ...

  5. python--利用列表推导式快速生成xml格式数据

    在接口自动化测试中,我们经常将要发送的数据放到excel里. json数据放至excel方便,但最近的一个测试,数据是xml格式发送的 如下: 属性 必选/可选 描述 1. Message Eleme ...

  6. Linux shell一行流编程实践

    Linux下很多命令用起来真相当方便,尤其是进行批处理操作时.(话说感觉这种程序也不复杂,windows咋一直不搞一个好用的shell呢) 这里列出一些实际shell操作的应用场景,具体命令的用法与解 ...

  7. iOS - UIEvent事件及UIResponder响应者

    在iOS中不是所有的对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,称之为响应者对象: UIApplication.UIViewController.UIView都继承自U ...

  8. Shell for

    for循环一般格式为:for 变量 in 列表do command1 command2 ... commandNdone列表是一组值(数字.字符串等)组成的序列,每个值通过空格分隔.每循环一次,就将列 ...

  9. Oracle备份恢复之逻辑备份

    exp 交互模式:导出scott用户下的emp表. [oracle@localhost ~]$ exp Export: Release 10.2.0.1.0 - Production on Thu N ...

  10. 币安Binance API Websocket

    本文介绍币安Binance API Websocket General WSS information The base endpoint is: wss://stream.binance.com:9 ...