patternjavaMinor
Permutation of given string
Viewed 0 times
permutationstringgiven
Problem
I am reading a book on DSA, and the author explains how to generate the permutation of strings using recursion. I want to know if there are better ways of doing the same, unless this is the best solution. Also, could someone help me understand the time-complexity of this algorithm? Recursion is something I use very rarely but am trying to learn about now.
public class Anagrams {
private char[] chArray;
private int size;
private static int count;
public Anagrams(String anagramString, int size) {
if (size <= 0) {
System.out.println("Please enter a valid String");
}
chArray = anagramString.toCharArray();
this.size = size;
}
public static void main(String args[]) {
String str = "dogs";
Anagrams anagramGenerator = new Anagrams(str, str.length());
anagramGenerator.generateAnagrams(str.length());
}
public void generateAnagrams(int newSize) {
if (newSize == 1)
return;
for (int i = 0; i < newSize; i++) {
generateAnagrams(newSize - 1);
if (newSize == 2)
displayWord();
rotate(newSize);
}
}
private void rotate(int newSize) {
// TODO Auto-generated method stub
int position = size - newSize;
char tempCh = chArray[position];
int i;
for (i = position + 1; i < size; i++) {
chArray[i - 1] = chArray[i];
}
chArray[i - 1] = tempCh;
}
private void displayWord() {
// TODO Auto-generated method stub
String word = "";
count++;
for (int i = 0; i < chArray.length; i++) {
word += chArray[i];
}
System.out.println(count + ")" + word);
}
}Solution
Be consistent
The most important advice I could give you is be consistent. Be consistent with your naming convention, the placement of brackets and everything else. Why ? Because even if you're doing something wrong you will be predictable, and that is what make things faster when reviewing.
vs
Same situation, an
Constructor
You already using
Another important point, you're checking if the
Comments
Never leave
The most important advice I could give you is be consistent. Be consistent with your naming convention, the placement of brackets and everything else. Why ? Because even if you're doing something wrong you will be predictable, and that is what make things faster when reviewing.
if (size <= 0) {
System.out.println("Please enter a valid String");
}vs
if (newSize == 1)
return;Same situation, an
if condition and one line of code if the condition is good. Why one have brackets and the other one don't have it ? In this particular case, always use brackets. It's more readable, if you add a line of code you don't have to add brackets and it will probably reduce the chance to have bugs.Constructor
public Anagrams(String anagramString, int size) You already using
String as parameter, why do you have size ? When you call your constructor, you're passing str.lenght(). I suggest that you drop the second argument and just call the length directly in your constructor.public Anagrams(String anagramString) {
if (size <= 0) {
System.out.println("Please enter a valid String");
}
chArray = anagramString.toCharArray();
this.size = anagramString.lenth();
}Another important point, you're checking if the
size <= 0 and print that you must enter a valid String, but you don't do nothing else. The program will go on in an invalid state. You could throw an exception that specify that the length of the String must be greater than 0. Comments
Never leave
// TODO Auto-generated method stub comments. It's just noise in the code, and could lead to people thinking that your code is somewhat not finished.Code Snippets
if (size <= 0) {
System.out.println("Please enter a valid String");
}if (newSize == 1)
return;public Anagrams(String anagramString) {
if (size <= 0) {
System.out.println("Please enter a valid String");
}
chArray = anagramString.toCharArray();
this.size = anagramString.lenth();
}Context
StackExchange Code Review Q#29712, answer score: 3
Revisions (0)
No revisions yet.