Given two strings, write a method to decide if one is a permutation of the other.

Example

abcd is a permutation of bcad, but abbe is not a permutation of abe

 public class Solution {
/**
* @param A a string
* @param B a string
* @return a boolean
*/
public boolean stringPermutation(String A, String B) {
// Write your code here
if(A==null&&B==null){
return true;
} else if (A==null||B==null||A.length()!=B.length()){
return false;
}
Map<Character, Integer> a = new HashMap<Character, Integer>(); for(char c : A.toCharArray()){
if(!a.containsKey(c))
a.put(c, 1);
else
a.put(c, a.get(c)+1); //a.put(c, a.getOrDefault(c, 0) + 1);
}
for(char c : B.toCharArray()){
if(!a.containsKey(c) || a.get(c)==0){
return false;
}
a.put(c, a.get(c)-1);
}
return true;
}
}

String Permutation的更多相关文章

  1. 28. 字符串的全排列之第2篇[string permutation with repeating chars]

    [本文链接] http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html [题目] 输入一个字符串 ...

  2. string permutation with upcase and lowcase

    Give a string, which only contains a-z. List all the permutation of upcase and lowcase. For example, ...

  3. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

  4. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

  5. Permutation Sequence LT60

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. Java--剑指offer(6)

    26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. /** public class TreeNode { int val = 0 ...

  8. java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序

    import java.util.ArrayList;import java.util.Collections;import java.util.List; public class Test7{   ...

  9. 剑指offer习题集2

    1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...

随机推荐

  1. centos----------centos下如何安装phpstorm

    1.首先打开centos下的谷歌浏览器,找到phpstorm官网下载linux版本.PhpStorm-2016.3.2.tar.gz 2.然后gunzip PhpStorm-2016.3.2.tar. ...

  2. python将对象名的字符串类型,转化为相应对象的操作方法

    在实际使用Python的过程中,遇到了一个问题,就是定义一个数组,数组内容为对应类名字的字符串. 此时在调用对应的类,生成实例时,需要将字符串转化为相应的类,之后再进行实例化. # coding : ...

  3. 安装archlinux的linux命令记录

    磁盘的分区:cfdisk 格式化分区:mkfs.ext4,mkswap,swapon 查看所有分区:lsblk /dev/sda 先挂载 / 分区:mount /dev/sda1 /mnt archl ...

  4. keepalived 工作原理

    keepalived主要通过vrrp协议为基础进行通信 所以先从VRRP协议说起: VRRP: 英文全称 Virtual Router Redundancy Protocol, .中文:虚拟路由冗余协 ...

  5. 0004-20180422-自动化第五章-python基础学习笔记

    内容回顾:1.数据类型 2.for和while循环 continue break #如下循环将怎么打印结果? for i in range(1,10): print(i) for i in range ...

  6. Shift键的三个妙用!Word又现神操作!

    1.Shift+Alt+上下方向键 :调整段落顺序 同时按这三个键,能够调整段落的顺序,也可以用来调整表格中的行序. 2.Shift+F3:英文大写/小写/首字母大写,这三种模式切换 PS:如果中间夹 ...

  7. JavaScript 初知

    JavaScript 初知 1.JavaScript 独立的语言2.浏览器就是JavaScript的语言解释器3.JavaScript 代码存在于HTML中4.单行注释用“//”.多行注释 /* */ ...

  8. Shell 常见理论问答

    (1)shell脚本中,怎么可以把某一行注释掉? 答:“#”. (2)如何执行一个shell脚本呢? 答:“sh x.sh”,“加执行./x.sh”,“bash x.sh”. (3)为了方便管理我们约 ...

  9. 【题解】Luogu P2766 最长不下降子序列问题

    原题传送门 实际还是比较套路的建图 先暴力dp一下反正数据很小 第一小问的答案即珂以求出数列的最长不下降子序列的长度s 考虑第二问如何做: 将每个点拆点 从前向后连一条流量为1的边 如果以它为终点的最 ...

  10. pageUtil分页工具

    分页工具: https://www.cnblogs.com/ggq-insist-qiang/articles/10095603.html