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;
if (firstString.length() != secondString.length() return false;
\$\endgroup\$