If the arrays don't contain duplicates, one way to do this in O(N)
is to use a Set
that represents a canonical form of the strings in the array. Something like this:
static Set<String> canonicalSet(String[] arr) {
Set<String> upperSet = new HashSet<String>();
for (String s : arr) {
upperSet.add(s.toUpperCase());
}
return upperSet;
}
static boolean equalsCanonically(String[] arr1, String[] arr2) {
return canonicalSet(arr1).equals(canonicalSet(arr2));
}
This is time-optimal.
You can also do variations on this technique to save more space, e.g. instead of constructing the canonical sets and comparing them, you can construct the canonical set for arr1
, and then remove entries from that set according to elements of arr2
. It the set is empty afterward, and you can always find what you need to remove, the two arrays are canonically equal.
static boolean equalsCanonically2(String[] arr1, String[] arr2) {
Set<String> canon = canonicalSet(arr1);
for (String s : arr2) {
if (!canon.remove(s.toUpperCase())) return false;
}
return canon.isEmpty();
}
You can also do a simple size-comparison check if you think it's worth it (i.e. if often the two arrays don't even have the same number of elements).
If there are duplicates in the arrays, then the Set
method will not work as is. You'd need a multiset, and you can either implement your own, or use Google Collections'.
There are also O(N log N)
ways to do this involving sorting the strings. You can sort both arrays and then do a simple linear check. A case-insensitive comparator must be used, and in fact it's already there as String.CASE_INSENSITIVE_ORDER
.
static boolean equalsCanonically3(String[] arr1, String[] arr2) {
int N = arr1.length;
if (arr2.length != N) return false;
Arrays.sort(arr1, String.CASE_INSENSITIVE_ORDER);
Arrays.sort(arr2, String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < N; i++) {
if (String.CASE_INSENSITIVE_ORDER.compare(arr1[i], arr2[i]) != 0) {
return false;
}
}
return true;
}
This last technique works even if the arrays contain duplicates. It does it O(N log N)
. It sorts the arrays passed as parameters, so if the original state is important, you want to pass their clone()
instead.