211. String Permutation【LintCode by java】
Description
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 Permutation(String A, String B) {
// write your code here
if(A.length() != B.length())
return false;
char[]a = A.toCharArray();
char[]b = B.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
for(int i = 0; i < a.length; i++){
if(a[i] != b[i])
return false;
}
return true;
}
}
也可以通过哈希表来做:如果每个元素的个数都相等,并且总个数相同,则字符串完全相等,参考:https://blog.csdn.net/wutingyehe/article/details/51212982。
211. String Permutation【LintCode by java】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 212. Space Replacement【LintCode by java】
Description Write a method to replace all spaces in a string with %20. The string is given in a char ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 165. Merge Two Sorted Lists【LintCode by java】
Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...
- 157. Unique Characters 【LintCode by java】
Description Implement an algorithm to determine if a string has all unique characters. Example Given ...
- 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】
Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
随机推荐
- WebApiClient.AOT.dll 调用api地址 -> 调用方法
优点:简化api调用过程,WebApiClient.AOT.dll中的IHttpApi接口 缺点:只适用于内部服务之间的调用(没有验证过程) 1.继承IHttpAPi接口 public interfa ...
- 二、HDFS 架构
源自:http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html HDFS has a m ...
- HTML如何禁止input输入
第一种方法: 在input元素上加readonly="readonly"属性 <input type="text" readonly="read ...
- jQuery 属性操作 - addClass() 和 removeClass() 方法
实例 向第一个 p 元素添加一个类: $("button").click(function(){ $("p:first").addClass("int ...
- chromium之lazy_instance
先看看介绍 // The LazyInstance<Type, Traits> class manages a single instance of Type, // which will ...
- 在SQL Server中批量修改有规律列的定义
)=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...
- CentOS 7.x eth0
1:Test Environment [root@linux-node1 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server rel ...
- Hibernate 事务不回滚
问题: 这几天在做开发时,发现事务不回滚了,Service是用AOP加的事务,数据库是MySql, 表全部是InnoDB: 方法回滚是采用spring的手动回滚: ...
- QQ空间|qq人气号怎么赚钱?
回报,付出的终极诉求,咱不论情怀. 在<怎么做一个QQ人气号>中,笔者大致提及,打造人气空间的流程,这里简单剖析下QQ人气空间的盈利模式. 关键词:转让,出售,广告,微商,合作,网红,接推 ...
- flume搭建新手测试环境
硬件环境: 腾讯云,两台服务器8G 双核 软件环境: flume1.8.jdk1.8,centos6 第一次搭建也是各种找文件,只知道flume是日志抓取服务,也听说了非常稳定强大的服务,正好公司需要 ...