CC150-Array and String 1.3

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;
}

 

posted @ 2015-08-15 04:10  haochen_Mark  阅读(108)  评论(0编辑  收藏  举报