String Permutation with a few conditions
I need a Java algorithm for a String permutation with a few conditions:
- Every letter just once per word
- The word has to end with a certain String
- Only words that have a certain length should be shown.
- Every letter can be in lower and upper case.
For example:
String perm = "abcdefgh";
Word length should be 7 or 8 and it should a开发者_如何转开发lways end with "g"
or "gh".
Okay:
abcdefgh ABCdefgh ABCDEFGH acbdefgh abdcefg abcdefg
Not okay:
abc abcdeghf
I think there should be a badge called "hall monitor" for telling students to do their own homework. :)
Here's how I'd do it:
- Create an array of available characters from the perm string.
- Create the suffix string. E.g. 'g' or 'gh'.
- Remove from the character array each of the letters of the suffix.
- Destination string = ""
- For i=0 to MAX_LENGTH: {
- Add a random letter from the array to the destination string
- Remove that letter from the array }
- Add the suffix to the destination string.
Run this multiple times to get more permutations.
精彩评论