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. Install rapyuta Robot Cloud Engine on Ubuntu12.04

    Prepare on ubuntu12.04 sudo apt-get install vim Install fuerte ROS sudo sh -c 'echo "deb http:/ ...

  2. 在CI (Jenkins) 从机(服务器)上使用bat批处理执行自动构建任务时,输出NuGet还原失败的解决方案

    编译环境:Jenkins+MSBuilds 1.搜索本次构建的解决方案中的所有csproj后缀文件,打开后找到这一段代码,并且删除掉.如果没有,直接忽略跳过. <Import Project=& ...

  3. 4A Watermelon

    A. Watermelon time limit per test 1 second memory limit per test 64 megabytes input standard input o ...

  4. Flutter 控件之 Routes 和 Navigator. [PopupRoute]

    一个 App 通常会有多个界面,每个界面实现不同的功能,并在多个界面之间跳转.在 Flutter 中多个界面的跳转是通过 Navigator 来实现的. 在 Flutter 中定义了一个 Overla ...

  5. 自制操作系统Antz(2)——进入保护模式 (上) jmp到保护模式

    Antz系统更新地址: https://www.cnblogs.com/LexMoon/category/1262287.htm Linux内核源码分析地址:https://www.cnblogs.c ...

  6. ubuntu 16.04 tip

    参考 1. 安装 python3.6 sudo add-apt-repository ppa:jonathonf/python-3.6  sudo apt-get update sudo apt-ge ...

  7. Container的简单认识

    容器是一个标准的软件单元,它将代码及其所有依赖关系打包,以便应用程序从一个计算环境快速可靠地运行到另一个计算环境. Docker容器映像是一个轻量级,独立的可执行软件包,包含运行应用程序所需的一切:代 ...

  8. CSS层叠样式表--使用

    一.颜色属性 二.字体属性三.背景属性四.文本属性五.边框属性六.列表属性七.display属性八.内外边距九.float属性十.定位十一.margin定位 一.颜色属性 (1)英文单词 <p ...

  9. centos6.5下安装tomcat

    linux在安装tomcat之前必须已安装jdk 已下载好tomcat 拖到centos系统的桌面 现在在桌面目录下 mv apache-tomcat-8.5.39.tar.gz /usr/local ...

  10. Java this关键字 学习笔记

    前言: 这篇博文就是系统的学习一下Java中的this关键字,本人对this关键字的理解知识简单的停留在对  类的成员变量进行赋值,这次所以决定系统的体会一下this 关键字 转自:https://b ...