开发者

String Permutation with a few conditions

开发者 https://www.devze.com 2023-02-01 15:50 出处:网络
I need a Java algorithm for a String permutation with a few conditions: Every letter just once per word

I need a Java algorithm for a String permutation with a few conditions:

  1. Every letter just once per word
  2. The word has to end with a certain String
  3. Only words that have a certain length should be shown.
  4. 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:

  1. Create an array of available characters from the perm string.
  2. Create the suffix string. E.g. 'g' or 'gh'.
  3. Remove from the character array each of the letters of the suffix.
  4. Destination string = ""
  5. For i=0 to MAX_LENGTH: {
  6. Add a random letter from the array to the destination string
  7. Remove that letter from the array
  8. }
  9. Add the suffix to the destination string.

Run this multiple times to get more permutations.

0

精彩评论

暂无评论...
验证码 换一张
取 消