【笔试】58、确定其中一个字符串的字符重新排列后,能否变成另外一个字符串

来源:互联网 发布:淘宝卖家申请电子面单 编辑:程序博客网 时间:2024/04/30 16:02

/**************************************************************************************** *题目:给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另外一个字符串 *时间:2015年10月18日19:25:13 *文件:Permutation.java *作者:cutter_point ****************************************************************************************/package bishi.Offer50.y2015.m10.d18;public class Permutation{public boolean change(String s1, String s2){//如果相等的话,首先长度肯定一样if(s1.length() != s2.length())return false;//我们用一个数组来统计相应的字符出现的次数int cNum1[] = new int[256];int cNum2[] = new int[256];//开始统计个数for(int i = 0; i < s1.length(); ++i){++cNum1[s1.charAt(i)];++cNum2[s2.charAt(i)];}//for//然后比较是否有不同的boolean result = true;for(int i = 0; i < 256; ++i){if(cNum1[i] != cNum2[i]){result = false;break;}//if}//forreturn result;}public static void main(String[] args){Permutation p = new Permutation();String s1 = "askjdhakf";String s2 = "askjdhakf";String s3 = "asda";String s4 = "";System.out.println(p.change(s1, s2));System.out.println(p.change(s1, s3));System.out.println(p.change(s1, s4));}}



结果:

true
false
false



0 0