String Permutation
Given two strings, write a method to decide if one is a permutation of the other.
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的更多相关文章
- 28. 字符串的全排列之第2篇[string permutation with repeating chars]
[本文链接] http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html [题目] 输入一个字符串 ...
- string permutation with upcase and lowcase
Give a string, which only contains a-z. List all the permutation of upcase and lowcase. For example, ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- Permutation Sequence LT60
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Java--剑指offer(6)
26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. /** public class TreeNode { int val = 0 ...
- java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序
import java.util.ArrayList;import java.util.Collections;import java.util.List; public class Test7{ ...
- 剑指offer习题集2
1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...
随机推荐
- MSG结构体和WndProc窗口过程详解
MSG结构体和WndProc窗口过程对于Windows编程非常重要,如果不了解它们,可以说就没有学会Windows编程. MSG结构体 MSG 结构体用来表示一条消息,各个字段的含义如下: typed ...
- 【Spark-core学习之三】 Spark集群搭建 & spark-shell & Master HA
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...
- 20190409Liunx中计划任务及压缩归档week2_day1
计划任务介绍 我们可以通过一些设置.来让电脑定时提醒我们该做什么事了.或者我们提前设置好,告诉电脑你几点做什么几点做什么,这种我们就叫它定时任务.而遇到一些需要执行的事情或任务.我们也可以通过命令来告 ...
- 传值与传引用(C++)
reference(引用) 是C++对C的一个扩充 int a; int &b = a;//声明b是一个整形的引用变量 C语言,函数的参数传递有2种形式:传值方式调用和传引用方式调用 传值方式 ...
- JavaScript 示例
JavaScript 示例 <html lang="en"> <head> <meta charset="UTF-8"> & ...
- Java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
在使用response重定向的时候,报以下错误:Java.lang.IllegalStateException: Cannot call sendRedirect() after the respon ...
- JS设计模式(11)中介者模式
什么是中介者模式? 中介者模式:对象和对象之间借助第三方中介者进行通信. 定义:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的 ...
- Bytom Java版本离线签名
比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom tx_s ...
- CSS【04】:CSS组合选择器
组合选择器 群组(并集)选择器 作用:给所有选择器选中的标签设置属性,可以同时控制多个选择器 格式: 选择器1, 选择器2 { 属性: 值; } 注意点: 必须使用,来连接 选择器可以使用标签名称.i ...
- mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法
mysql数据库连接出问题,提示超时,后来发现问题在于连接mysql数据库的jar包跟数据库版本不对应导致的,更换jar包一致就解决了.