Skip to main content
Question Protected by Jamal
deleted 69 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Can you offer a way to improve this Java code? Calculate all possible combinations of given characters

I was asked in my textbook "Lectures on Discrete Mathematics for Computer Science"Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c}{a,b,c} or any combination of characters {1,4,s,a}{1,4,s,a}) as well as a length value and calculate all possible combinations of this alphabet.

The code I have written is functional, however I'd like to read what things I am doing wrong or could be doing better. The book didn't give an example program, so I only hope this is what it was looking for, but maybe there's a much better way to do it or way to improve how I'm doing it. Maybe this is fine, but I just need someone to look at it and tell me in that case.

Improvement(s) or It's Fine = Great answer.

Thanks a lot!


 

Can you offer a way to improve this Java code?

I was asked in my textbook "Lectures on Discrete Mathematics for Computer Science" to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a length value and calculate all possible combinations of this alphabet.

The code I have written is functional however I'd like to read what things I am doing wrong or could be doing better. The book didn't give an example program so I only hope this is what it was looking for but maybe there's a much better way to do it or way to improve how I'm doing it. Maybe this is fine but I just need someone to look at it and tell me in that case.

Improvement(s) or It's Fine = Great answer.

Thanks a lot!


 

Calculate all possible combinations of given characters

I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a length value and calculate all possible combinations of this alphabet.

The code I have written is functional, however I'd like to read what things I am doing wrong or could be doing better. The book didn't give an example program, so I only hope this is what it was looking for, but maybe there's a much better way to do it or way to improve how I'm doing it. Maybe this is fine, but I just need someone to look at it and tell me in that case.

Post Migrated Here from stackoverflow.com (revisions)
Source Link
Phazor
Phazor

Can you offer a way to improve this Java code?

I was asked in my textbook "Lectures on Discrete Mathematics for Computer Science" to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a length value and calculate all possible combinations of this alphabet.

For example:

char[] alphabet = new char[] {'a','b'};
possibleStrings(3, alphabet,"");

This will output:

aaa
aab
aba
abb
baa
bab
bba
bbb

That is all combinations of the alphabet {a,b,c} with the string length set to 3.

The code I have written is functional however I'd like to read what things I am doing wrong or could be doing better. The book didn't give an example program so I only hope this is what it was looking for but maybe there's a much better way to do it or way to improve how I'm doing it. Maybe this is fine but I just need someone to look at it and tell me in that case.

Improvement(s) or It's Fine = Great answer.

Thanks a lot!


public class Program {
    
    public static void main(String[] args) {
        
        // Create an alphabet to work with
        char[] alphabet = new char[] {'a','b'};
        // Find all possible combinations of this alphabet in the string size of 3
        StringExcersise.possibleStrings(3, alphabet,"");
    }
    
} class StringExcersise {
        
    public static void possibleStrings(int maxLength, char[] alphabet, String curr) {
        
        // If the current string has reached it's maximum length
        if(curr.length() == maxLength) {
            System.out.println(curr);
        
        // Else add each letter from the alphabet to new strings and process these new strings again
        } else {
            for(int i = 0; i < alphabet.length; i++) {
                String oldCurr = curr;
                curr += alphabet[i];
                possibleStrings(maxLength,alphabet,curr);
                curr = oldCurr;
            }
        }
    }
}