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】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  3. 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 ...

  4. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  5. 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 ...

  6. 157. Unique Characters 【LintCode by java】

    Description Implement an algorithm to determine if a string has all unique characters. Example Given ...

  7. 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 ...

  8. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  9. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

随机推荐

  1. rabbitmq+topic+java

    可参照github代码:https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/EmitLogTopic.java 1. 新建m ...

  2. 为类型“xxxx”多次调用了 Map,且其中至少有一个调用未指定目标表名称。

    ef的继承映射真的是能搞死人啊...小结一下: 下图中的代码,ResponseTextMessage.ResponseNewsMessage.ResponseMethodMessage等几个类都是Re ...

  3. .Net core使用Quartz.Net 实现定时任务

    很多情况下,我们需要完成一些定时执行的功能,用很多定时工具,像:hangfire,TimeJob,以及Quartz.net,不过quartz.net 比较精确一些,功能也比较强大,所以我选择了Quar ...

  4. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

  5. 数据库与python的连接

    db=web.database( dbn="mysql", host="localhost", port=3306, user="root" ...

  6. 如何利用Linux去油管下载带字幕的优质英文资料提升英文听力和词汇量

    非常方便地从油管下载你需要的任何英文视频资料,并且带字幕,方便你学习某个特定领域的词汇: [step1,Centos6系统安装youtbe-dl下载带英文字幕的视频] 1.首先需要安装youtube- ...

  7. jQuery.qrcode 生成二维码,并使用 jszip、FileSaver 下载 zip 压缩包至本地。

    生成二维码 引用 jquery.qrcode.js  :连接:https://files.cnblogs.com/files/kitty-blog/jquery.qrcode.js .https:// ...

  8. 对于gitHub的总结随笔

    作用:用于项目的版本管理     密切相关的是       git                操作  1.本地的文件上传到github上                              ...

  9. Win7配置express4环境

    本机环境: $ node -v v10.14.1 $ npm -v 6.4.1 配置node环境变量: #配置全局安装目录 npm config set prefix "G:\WEB\nod ...

  10. ecshop 后台添加新菜单 以及 权限控制

    首先 在languages\zh_cn\admin\common.php 中添加 一级菜单 二级菜单 其次 在admin\includes\inc_menu.php 中添加 然后 在admin\inc ...