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 ->
Maybe it's not the fastest answer, but it must be 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 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");
}
Agree to what @Jean says above for an efficient solution using HashMap. This problem is also called Anagram. Below is the solution in Scala.
Note: cleanString is where whitespaces are removed and all characters are lowercase
def isAnagram(cleanString1, cleanString2) = {
createHashMap(cleanString1) == createHashMap(cleanString2)
}
def createHashMap(str: String): immutable.HashMap[Char, Int] = {
str.foldLeft(immutable.HashMap.empty[Char, Int]) { (acc, next)
=> if (acc.contains(next)) acc + (next -> (acc(next) + 1))
else acc + (next -> 1)
}
}
Turn each string into a char[], sort that array, then compare the two.
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);
}
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.
This problem can be simply solved in O(n) time and O(1) space. The idea is to use a temp array of size 26 as we have only 26 characters in the alphabet.
First, if length of both strings are different we immediately return false. We iterate over length of the given string and in temp array, increase frequency of every character in string one and decrease count of character occured in other string. At the end temp array should have 0 count for every character if strings have equal characters.