问题描述:如题,废话很少说,上菜。java
/** * 这里规定大小写为不一样字符,且考虑字符串重点空格。 * 保证两串的长度都小于等于5000。 * 思路:全部字符总数为256,定义两个256的数组,下标分别表示字符的ASCII值,对应数值表示出现次数, * 统计AB字符串后遍历两个数组比较对应位置数值是否相同便可。 * @param stringA * @param stringB * @return */
public boolean checkSam(String stringA, String stringB) {
//两字符串存在空的状况
if (stringA == null || stringB == null){
return false;
}
/** * 两字符串都不为空 * 长度不相等则直接返回false */
if (stringA.length() != stringB.length()){
return false;
}
int[] strA = new int[256];
int[] strB = new int[256];
for(int i = 0; i < stringA.length(); i++){
strA[stringA.charAt(i)]++;
strB[stringB.charAt(i)]++;
}
//遍历数组全部元素,进行对比
for (int i = 0; i < 256; i++){
if (strA[i] != strB[i]){
return false;
}
}
return true;
}
感受这题还不错,记录下。web