标签:
problem:
Given two string, write a method to decide if one is a permutation of the other
Solution:
1. sort two strings and return weather str1 equal to str2
2. record number of char of str1 and detract str2 weather equal to zero for every items.
code:
public static boolean changable(String str1, String str2){
if(str1.length()!=str2.length()) return false;
int[] index = new int[256];
for(int i =0; i<str1.length();i++){
int val = str1.charAt(i);
index[val]++;
}
for(int i=0; i<str2.length();i++){
int val = str2.charAt(i);
index[val]--;
}
for (int i = 0; i < index.length; i++) {
if(index[i]!=0)
return false;
}
return true;
}
标签:
原文地址:http://www.cnblogs.com/whaochen/p/4731638.html