标签:HERE TE 数组 length eth end line https red
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
解题:遇到过类似的题目,比较简单的方法是,把字符串转化为字符数组,然后排序、比较每一位的数是否相等。这样做效率比较低,代码如下:
1 public class Solution { 2 /** 3 * @param A: a string 4 * @param B: a string 5 * @return: a boolean 6 */ 7 public boolean Permutation(String A, String B) { 8 // write your code here 9 if(A.length() != B.length()) 10 return false; 11 char[]a = A.toCharArray(); 12 char[]b = B.toCharArray(); 13 Arrays.sort(a); 14 Arrays.sort(b); 15 for(int i = 0; i < a.length; i++){ 16 if(a[i] != b[i]) 17 return false; 18 } 19 return true; 20 } 21 }
也可以通过哈希表来做:如果每个元素的个数都相等,并且总个数相同,则字符串完全相等,参考:https://blog.csdn.net/wutingyehe/article/details/51212982。
211. String Permutation【LintCode by java】
标签:HERE TE 数组 length eth end line https red
原文地址:https://www.cnblogs.com/phdeblog/p/9214272.html