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. linux关闭终端响铃

    title: linux关闭终端响铃 date: 2018-01-25 15:10:14 tags: linux categories: linux 在终端输入或是直接在.bashrc里添加一行 xs ...

  2. SLF4J bindings

    see: https://www.slf4j.org/manual.html

  3. 微信小程序上传与下载文件

    需要准备的工作: ①.建立微信小程序工程,编写以下代码. ②.通过IDE建立springboot+web工程,编写接收文件以及提供下载文件的方式,并将上传的文件相关信息记录在mysql数据库中.具体请 ...

  4. Bugku-CTF之变量1

    Day9 变量1 http://123.206.87.240:8004/index1.php      

  5. Bootstrap3基础 table-bordered/hover 表格加外边框和鼠标悬停对应行的背景色加深

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  6. WebApi实现验证授权Token,WebApi生成文档等(转)

    using System; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Security; ...

  7. 代码覆盖率-JaCoCo

    代码覆盖率 在做单元测试时,代码覆盖率常常被拿来作为衡量测试好坏的指标,甚至,用代码覆盖率来考核测试任务完成情况,比如,代码覆盖率必须达到80%或 90%. JaCoCo Jacoco从多种角度对代码 ...

  8. JS开发工具WebStorm使用快捷键

    快捷键可以提高开发效率,最好用的就是这些! 代码编辑 Ctrl + d   复制整行 Ctrl + '-/+'  模块折叠 Ctrl + [ ]   括号匹配 Ctrl + F12 结构展示 Shif ...

  9. Vue:(五)axios

    Axios是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.axios主要是用于向后台发起请求的,还有在请求中做更多可控功能.官方不再维护vue-resource,推 ...

  10. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...