码迷,mamicode.com
首页 > 其他好文 > 详细

CC150-Array and String 1.3

时间:2015-08-15 06:45:36      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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

 

CC150-Array and String 1.3

标签:

原文地址:http://www.cnblogs.com/whaochen/p/4731638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!