这道题是这样的,有两个字符串,比较他们中间的值是否相等.注意不是一般的相等!例如
String stra = "ccaaiub"
String strb = "iubacac"
如果出现这样的情况也是相等的!
也就是说两个字符串中字符的个数相等,每个字符出现的次数也是相等的,有什么好的解决方案,考虑下效率上面的因素,因为字符串是不定长的
[b]问题补充:[/b]
如果字符串中穿插的有其它字符,比如数字123等等!给点伪代码,大家都看看!
这道题是这样的,有两个字符串,比较他们中间的值是否相等.注意不是一般的相等!例如
String stra = "ccaaiub"
String strb = "iubacac"
如果出现这样的情况也是相等的!
也就是说两个字符串中字符的个数相等,每个字符出现的次数也是相等的,有什么好的解决方案,考虑下效率上面的因素,因为字符串是不定长的
[b]问题补充:[/b]
如果字符串中穿插的有其它字符,比如数字123等等!给点伪代码,大家都看看!
[code="java"]
import java.util.*;
public class StringCompare
{
/**
* TODO 方法说明
*
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
String stra = "ccaaiub" ;
String strb = "iubacac" ;
System.out.println(compare(stra,strb));
}
public static boolean compare(String a, String b)
{
if(a == null && b == null)
return true;
if(a == null || b == null)
return false;
if(a.equals(b))
return true;
if(a.length() != b.length())
return false;
byte[] byteA = a.getBytes();
byte[] byteB = b.getBytes();
Map<Byte,Integer> mapA = new HashMap<Byte, Integer>();
Map<Byte,Integer> mapB = new HashMap<Byte, Integer>();
Byte btA,btB;
for(int i=0;i<byteA.length;i++)
{
btA = byteA[i];
btB = byteB[i];
if( mapA.get(btA) == null )
mapA.put(btA, 1);
else
mapA.put(btA, mapA.get(btA) + 1);
if( mapB.get(btB) == null )
mapB.put(btB, 1);
else
mapB.put(btB, mapB.get(btB) + 1);
}
if(mapA.equals(mapB))
return true;
else
return false;
}
}
[/code]