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

/****************************************************************************************
 *题目:给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另外一个字符串
 *时间: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
		}//for
		
		return 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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值