Is there a way to check if two strings contain the same characters. For example,
abc, bca -> true
aaa, aaa -> true
aab, bba -> false
abc, def -> false
Is there a way to check if two strings contain the same characters. For example,
abc, bca -> true
aaa, aaa -> true
aab, bba -> false
abc, def -> false
Turn each string into a char[], sort that array, then compare the two. Simple.
private boolean sameChars(String firstStr, String secondStr) {
char[] first = firstStr.toCharArray();
char[] second = secondStr.toCharArray();
Arrays.sort(first);
Arrays.sort(second);
return Arrays.equals(first, second)
}
You can convert the string into char array, sort the arrays and them compare the arrays:
String str1 = "abc";
String str2 = "acb";
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);
if(Arrays.equals(chars1,chars2)) {
System.out.println(str1 + " and " + str2 + " are anagrams");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams");
}
here:
String str1 = "abc";
String str2 = "cba";
/* create sorted strings */
/* old buggy code
String sorted_str1 = new String( java.utils.Arrays.sort(str1.toCharArray()) );
String sorted_str2 = new String( java.utils.Arrays.sort(str2.toCharArray()) );
*/
/* the new one */
char [] arr1 = str1.toCharArray();
char [] arr2 = str2.toCharArray();
java.utils.Arrays.sort(arr1);
java.utils.Arrays.sort(arr2);
String sorted_str1 = new String(arr1);
String sorted_str2 = new String(arr2);
if (sorted_str1.equals( sorted_str2 ) ) {
/* true */
} else {
/* false */
}
A very easy - but not very efficient - way to do that is, convert your String
s to char arrays and use java.util.Arrays.sort on them, get String
s back and compare for equality.
If your strings are under a few thousand characters, that should be very okay.
If you have several megabytes strings, you may want to create an array with a count for each character (using its code as an index), have one pass on one string adding one on the count of each char, and one pass on the second string removing one. If you fall under 0 at any point during the second pass, they don't have the same characters. When you're done with the second string without error, you are sure they have the same characters if they have the same length (which you should have checked first anyway).
This second method is much more complicated than sorting the strings, and it requires a big array if you want to work with unicode strings, but it's perfectly good if you're okay with only the 128 chars of the ascii set, and much faster.
Do NOT bother with that if you don't have several million characters in your strings. Sorting the strings is much easier, and not significantly slower on strings with only a couple dozen chars.
As a (nitpicking ;-) ) side note:
Be aware that the solutions proposed here only work for strings composed of characters from the Basic Multilingual Plane (BMP) of Unicode.
Characters outside the BMP are represented as a pair of char
in a String
, so you need to pay extra attention, so you keep the pairs together. See the Javadocs of java.lang.Character
for the gory details.
Fortunately, most characters outside the BMP are rather exotic. Even most of Japanese and Chinese is in the BMP...
Consider creating a signature for a given String. Using count and character.
a-count:b-count:c-count:.....:z-count:
(extend for upper case if you want ).
Then compare the signature. This should scale better for very large Strings.
As a shortcut, check the length. If they are not matching, return false anyway.
Maybe it's not the fastest answer, but must shortest answer.
boolean hasSameChar(String str1, String str2){
for(char c : str1.toCharArray()){
if(str2.indexOf(c) < 0 ) return false;
}
for(char c : str2.toCharArray()){
if(str1.indexOf(c) < 0 ) return false;
}
return true;
}
You could use the communative properties of addition and treat each character numerically, adding each element of each array into a total figure.
If the totals are the same then they contain the same charaters.
You can thus avoid a possibly expensive sort on each char array.
bool equal = false;
if(strA.length() == strB.length()) {
long totalA = 0;
long totalB = 0;
for(int i = 0; i < strA.length(); i++) {
totalA += (int)strA.charAt(i);
totalB += (int)strB.charAt(i);
}
equal = totalA == totalB;
}
return equal;