patternjavaModerate
Check if two strings are permutations of one another
Viewed 0 times
aretwooneanothercheckstringspermutations
Problem
I am working my way through "Cracking the coding interview" problems and have finished implementing this question: "Given, two strings, write a method to decide if one is a permutation of the other". Can anyone give me any feedback on my implementation in regards of efficiency or improvements?
public static boolean containsPremutation(String firstString, String secondString){
//Checking if either string is empty.
if(firstString.isEmpty() || secondString.isEmpty())
return false;
//Checking if one string is larger than the other.
if(firstString.length() > secondString.length() || secondString.length() > firstString.length())
return false;
//First convert the Strings into char[]
char [] charFirstString = firstString.toCharArray();
char [] charSecondString = secondString.toCharArray();
//Secondly, aplhabetize/sort the char arrays
Arrays.sort(charFirstString);
Arrays.sort(charSecondString);
//Thirdly, create new Strings out of the sorted char arrays
String alphaFirstString = new String(charFirstString);
String alphaSecondString = new String(charSecondString);
//Now you can begin comparing each char in the Strings.
// Begin iterating at the same char and if they are not the same char return false
//otherwise continue iterating.
for (int i = 0; i < alphaFirstString.length(); i++){
if(alphaFirstString.charAt(i) != alphaSecondString.charAt(i))
return false;
}
return true;Solution
This question has used the word "permutation" for two strings, and in standard English what that's asking for is "is one the anagram of the other"? Using "anagram" as a search would help you find a lot of other information on this problem, especially on Code Review.
Your solution is significantly over-engineered. You have broken down the problem in to too many steps, and you've missed an opportunity to show how you would avoid code duplication.
Let's break down your code in to its stages:
Now, let's strip out the redundant parts... for example, you don't need to convert the sorted characters back to a string only to loop over each character again.... get rid of stage 5 and convert stage 6 to operate over the sorted array.
Also, stage 1, that's a bug. Two empty strings are anagrams of each other... agreed? Get rid of that check.
Now, what's left is two identical operations performed on two different inputs, which are then compared, so extract the common logic in to a function (Code reuse is a good thing):
Then, your code looks like:
Now what? Instead of converting them to strings, we can use the core library's
The full code would be:
Looking at the code that way, you can see other issues too...
-
do we even need the variables in the contains... method? Could it just be:
-
As mentioned in a comment, it's
Your solution is significantly over-engineered. You have broken down the problem in to too many steps, and you've missed an opportunity to show how you would avoid code duplication.
Let's break down your code in to its stages:
- Check each string is valid input
- Check the strings are the same length
- Extract the characters from each string
- Sort each string's characters
- Create a new String from each input's sorted characters.
- Iterate over each of the sorted string's characters and compare to the other string.
- Return the match status
Now, let's strip out the redundant parts... for example, you don't need to convert the sorted characters back to a string only to loop over each character again.... get rid of stage 5 and convert stage 6 to operate over the sorted array.
Also, stage 1, that's a bug. Two empty strings are anagrams of each other... agreed? Get rid of that check.
Now, what's left is two identical operations performed on two different inputs, which are then compared, so extract the common logic in to a function (Code reuse is a good thing):
private static final char[] sortedChars(String input) {
char [] chars = input.toCharArray();
Arrays.sort(chars);
return chars;
}Then, your code looks like:
public static boolean containsPremutation(String firstString, String secondString){
char[] firstChars = sortedChars(firstString);
char[] secondChars = sortedChars(secondString);
....
}Now what? Instead of converting them to strings, we can use the core library's
Arrays class to do the comparisons for us:return Arrays.equals(firstSorted, secondSorted);The full code would be:
private static final char[] sortedChars(String input) {
char [] chars = input.toCharArray();
Arrays.sort(chars);
return chars;
}
public static boolean containsPremutation(String firstString, String secondString){
char[] firstChars = sortedChars(firstString);
char[] secondChars = sortedChars(secondString);
return Arrays.equals(firstSorted, secondSorted);
}Looking at the code that way, you can see other issues too...
- generally, "Hungarian Notation" is frowned on in Java Code Style - don't have
Stringappended to variable names likefirstStringandsecondString... we know that they are strings.
-
do we even need the variables in the contains... method? Could it just be:
public static boolean containsPremutation(String first, String second){
return Arrays.equals(sortedChars(first), sortedChars(second));
}-
As mentioned in a comment, it's
Permutation and not Premutation.Code Snippets
private static final char[] sortedChars(String input) {
char [] chars = input.toCharArray();
Arrays.sort(chars);
return chars;
}public static boolean containsPremutation(String firstString, String secondString){
char[] firstChars = sortedChars(firstString);
char[] secondChars = sortedChars(secondString);
....
}return Arrays.equals(firstSorted, secondSorted);private static final char[] sortedChars(String input) {
char [] chars = input.toCharArray();
Arrays.sort(chars);
return chars;
}
public static boolean containsPremutation(String firstString, String secondString){
char[] firstChars = sortedChars(firstString);
char[] secondChars = sortedChars(secondString);
return Arrays.equals(firstSorted, secondSorted);
}public static boolean containsPremutation(String first, String second){
return Arrays.equals(sortedChars(first), sortedChars(second));
}Context
StackExchange Code Review Q#156336, answer score: 11
Revisions (0)
No revisions yet.