采用设置标志数组的方法,一定程度上降低了时间复杂度;
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;
}
int [] a = new int [256];
for (int i = 0; i<a.length ; i++){//数组初始化
a[i] =0;
}
for (int i = 0; i<A.length() ; i++){
int temp=(int) A.charAt(i);
a[temp] =a[temp]+1;
}
for (int i = 0; i< B.length() ; i++){
int temp=(int) B.charAt(i);
a[temp] =a[temp]-1;
}
for (int i =0; i<256;i++){
if(a[i] != 0){
return false;
}
}
return true;
}
}