@XQF
2018-03-07T22:59:13.000000Z
字数 861
阅读 1189
数据结构与算法
看清,。,。不是问的是不是相等,相等只是其中一种情况.假如不是需要每个相同字符的个数也一样的话比较麻烦,倘若要求一样那就直接排序后euqals
public class Solution {
public boolean hasEqualChars(String a, String b) {
if (a == null || b == null) {
return false;
}
if (a.equals(b)) {
return true;
}
char[] charA = a.toCharArray();
char[] charB = b.toCharArray();
int len = 'z' - 'A' + 1;
int[] book = new int[len];
boolean result = true;
out:
for (char i = 'A'; i <= 'z'; i++) {
int temp = i - 'A';
for (int j = 0; j < charA.length; j++) {
if (i == charA[j]) {
book[temp] = 1;
}
}
for (int j = 0; j < charB.length; j++) {
if (i == charB[j]) {
if (book[temp] == 1) {
book[temp] = 0;
} else {
result = false;
break out;
}
}
}
}
for (int i : book) {
if (i != 0) {
result = false;
}
}
return result;
}
public static void main(String[] args) {
Solution solution = new Solution();
String a = "Aardr";
String b = "Aadr";
System.out.println(solution.hasEqualChars(a, b));
}
}