字符串置换

描述:

给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。

置换的意思是,通过改变顺序可以使得两个字符串相等。

样例:

"abc""cba" 的置换。

"aabc" 不是 "abcc" 的置换。

思路:

误区是列出给定字符串的全部置换字符串然后再一一比对,这样肯定会超时。

根据题目给出的置换的概念和样例可以知道几个条件:

  1. 两个字符串长度相同。
  2. 每个字符串中的各个字符数量相同。

因此,可以直接对比两个字符串的长度、每个字符的出现次数,故代码如下:

代码:

import java.util.HashMap;
import java.util.Map;
public class Solution {
/**
* @param A a string
* @param B a string
* @return a boolean
*/
public static boolean stringPermutation(String A, String B) {
// Write your code here
if (A.equals(B)) {
return true;
}
if (A.length() == B.length()) {
return mapCompare(charCount(A), charCount(B));
}
return false;
}
// 得到字符串各个字符的出现次数
public static Map<Character, Integer> charCount(String str) {
Map<Character, Integer> map = new HashMap<>();
char[] arr = str.toCharArray();
for (char item : arr) {
if (map.containsKey(item)) {
Integer num = map.get(item);
num += 1;
map.put(item, num);
} else {
map.put(item, 1);
}
}
return map;
}
// 比较两个map
public static boolean mapCompare(Map<Character, Integer> map1, Map<Character, Integer> map2) {
if (map1.size() != map2.size()) {
return false;
}
for (Character c : map1.keySet()) {
try {
if (map1.get(c) != map2.get(c)) {
return false;
}
} catch (NullPointerException e) {
// 说明map2中没有map1中的某个字符
return false;
}
}
return true;
}
}

【LintCode·容易】字符串置换的更多相关文章

  1. LintCode-211.字符串置换

    字符串置换 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换. 置换的意思是,通过改变顺序可以使得两个字符串相等. 样例 "abc" 为 "cb ...

  2. lintcode:字符串置换

    题目 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换. 置换的意思是,通过改变顺序可以使得两个字符串相等. 样例 "abc" 为 "cba&q ...

  3. HihoCoder - 1801 :剪切字符串 (置换与逆序对)

    Sample Input 6 5 11 Sample Output 6 小Hi有一个长度为N的字符串,这个字符串每个位置上的字符两两不同.现在小Hi可以进行一种剪切操作: 选择任意一段连续的K个字符, ...

  4. lintcode :同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. lintcode :旋转字符串

    题目: 旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdef ...

  6. LintCode翻转字符串问题 - python实现

    题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...

  7. LintCode——交叉字符串

    描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...

  8. LintCode——旋转字符串

    描述:给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例:对于字符串 "abcdefg"     offset=0 => "abcdefg&qu ...

  9. [LintCode]转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

随机推荐

  1. javascript倒计时调转页面

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  2. LeetCode 485. Max Consecutive Ones (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  3. c# Invoke和Begininvoke区别

    一.对Invoke和Begininvoke的认识 1.Invoke():同步委托,会阻塞当前主线程的运行,等待invoke()方法返回才执行后面的代码: 2.Begininvoke():异步委托,调用 ...

  4. 树的三种遍历方式(C语言实现)

    //************************************************************************* // [前序]遍历算法 //二叉树不空,先访问根 ...

  5. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...

  6. DOM 对象

    DOM  ==  document object model   document 对象是唯一同时属于  BOM 和 DOM  的   rows 是一种DOM集合,不是数组,所以没有sort() 函数 ...

  7. Linux修改时区的正确方法

    CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ll /etc/localti ...

  8. redis数据库安装及简单的增删改查

    redis下载地址:https://github.com/MSOpenTech/redis/releases. 解压之后,运行 redis-server.exe redis.windows.conf  ...

  9. JDBC连接数据库的几种方法

    一. 最古老的方法(通过 Driver 接口直接连接数据库) 首先创建一个 Driver 实现类的对象 Driver dirver = new com.mysql.jdbc.Driver(); 准备连 ...

  10. 使用Docker安装Mysql

    最近使用阿里云服务器,学习一下Docker,今天学着使用Docker安装MySQL. 首先,从阿里云的Docker Hub 上pull一个MySQL的image. [centos@loovelj~]$ ...