Каждая комбинация символьного массива - PullRequest
0 голосов
/ 12 марта 2012

Возникают проблемы при попытке показать каждую комбинацию символов массива без повторяющихся букв.

public static String[] getAllLists(String[] elements, int lengthOfList)
{
    //initialize our returned list with the number of elements calculated above
    String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];

    //lists of length 1 are just the original elements
    if(lengthOfList == 1) return elements; 
    else
    {
        //the recursion--get all lists of length 3, length 2, all the way up to 1
        String[] allSublists = getAllLists(elements, lengthOfList - 1);

        //append the sublists to each element
        int arrayIndex = 0;

        for(int i = 0; i < elements.length; i++)
        {
            for(int j = 0; j < allSublists.length; j++)
            {
                //add the newly appended combination to the list
                allLists[arrayIndex] = elements[i] + allSublists[j];
                arrayIndex++;
            }
        }

        return allLists;
    }
}

Приведенный выше код работает отлично, но используйте каждую букву более одного раза, чего нельзя сделать в этом случае.

И я застрял, как это сделать сейчас.

1 Ответ

7 голосов
/ 15 марта 2012

Вот пример реализации.По сути, он принимает строку и перебирает каждый символ, помещая этот символ впереди.Затем он возвращается к оставшимся персонажам.Эта структура устраняет проблему повторяющихся букв, поскольку при вводе в рекурсию удаляется уже использованный вами символ.

Я также сохранил результаты в наборе для удаления семантических эквивалентностей.Вход «aab» может переключать char 0 и char 1, но все равно быть «aab»Я использовал TreeSet для сохранения порядка для более легкой проверки вывода, но HashSet был бы быстрее.

  public static Set<String> permute(String chars)
  {
    // Use sets to eliminate semantic duplicates (aab is still aab even if you switch the two 'a's)
    // Switch to HashSet for better performance
    Set<String> set = new TreeSet<String>();

    // Termination condition: only 1 permutation for a string of length 1
    if (chars.length() == 1)
    {
      set.add(chars);
    }
    else
    {
      // Give each character a chance to be the first in the permuted string
      for (int i=0; i<chars.length(); i++)
      {
        // Remove the character at index i from the string
        String pre = chars.substring(0, i);
        String post = chars.substring(i+1);
        String remaining = pre+post;

        // Recurse to find all the permutations of the remaining chars
        for (String permutation : permute(remaining))
        {
          // Concatenate the first character with the permutations of the remaining chars
          set.add(chars.charAt(i) + permutation);
        }
      }
    }
    return set;
  }

Пример выполнения:

  public static void main(String[] args)
  {
    for (String s : CharPermuter.permute("abca"))
    {
      System.out.println(s);
    }
  }

Генерирует:

aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...