Shuffling a string so that no two adjacent letters are the same

NGambit

I've been trying to solve this interview problem which asks to shuffle a string so that no two adjacent letters are identical For example,

ABCC -> ACBC

The approach I'm thinking of is to

1) Iterate over the input string and store the (letter, frequency) pairs in some collection

2) Now build a result string by pulling the highest frequency (that is > 0) letter that we didn't just pull

3) Update (decrement) the frequency whenever we pull a letter

4) return the result string if all letters have zero frequency

5) return error if we're left with only one letter with frequency greater than 1

With this approach we can save the more precious (less frequent) letters for last. But for this to work, we need a collection that lets us efficiently query a key and at the same time efficiently sort it by values. Something like this would work except we need to keep the collection sorted after every letter retrieval.

I'm assuming Unicode characters.

Any ideas on what collection to use? Or an alternative approach?

Sergey Kalinichenko

You can sort the letters by frequency, split the sorted list in half, and construct the output by taking letters from the two halves in turn. This takes a single sort.

Example:

  • Initial string: ACABBACAB
  • Sort: AAAABBBCC
  • Split: AAAA+BBBCC
  • Combine: ABABABCAC

If the number of letters of highest frequency exceeds half the length of the string, the problem has no solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return a list of string letters grouped by two

From Dev

Regex - how to match two or four same letters but not three

From Dev

RegEx: find every string with two or more letters

From Dev

Two adjacent lists in python

From Dev

Check if a string is build out of the same letters as another string

From Dev

Swap two letters in a string

From Dev

Count All Permutations Where No Two Adjacent Characters Are the Same

From Dev

Add space between two letters in a string in R

From Dev

Problems with shuffling characters in a string

From Dev

Shuffling two lists separately in Python

From Dev

Saying that two letters have the same quantifier without specifying a number in Regex

From Dev

Get first letters from a string of two words (PHP - fastest way)

From Dev

How can I check if two strings have the same letters, but only print the common letters once?

From Dev

adjacent letters in strings

From Dev

Arrange eight consecutive number in a matrix so that no two number are adjacent

From Dev

CSS: Display only the first two letters of a string

From Dev

Reverse only letters in string and keep the same order of the words

From Dev

How to iterate letters in every string in the list at the same time?

From Dev

Random shuffling of String array

From Dev

RegEx: find every string with two or more letters

From Dev

Check if a string is build out of the same letters as another string

From Dev

Count number of same letters in a string

From Dev

How would I check to see if two strings contain the same letters

From Dev

Finding the number of matching letters in two different string at the same indices

From Dev

Check if string contains three same letters in Octave

From Dev

Remove words from file which has two or more of the same letters letters in a row

From Dev

Swap occurrences of two most frequent letters in a string

From Dev

Make two divs of variable width adjacent and centered at the same time

From Dev

Better way to swap two adjacent words in a PERL string?

Related Related

  1. 1

    Return a list of string letters grouped by two

  2. 2

    Regex - how to match two or four same letters but not three

  3. 3

    RegEx: find every string with two or more letters

  4. 4

    Two adjacent lists in python

  5. 5

    Check if a string is build out of the same letters as another string

  6. 6

    Swap two letters in a string

  7. 7

    Count All Permutations Where No Two Adjacent Characters Are the Same

  8. 8

    Add space between two letters in a string in R

  9. 9

    Problems with shuffling characters in a string

  10. 10

    Shuffling two lists separately in Python

  11. 11

    Saying that two letters have the same quantifier without specifying a number in Regex

  12. 12

    Get first letters from a string of two words (PHP - fastest way)

  13. 13

    How can I check if two strings have the same letters, but only print the common letters once?

  14. 14

    adjacent letters in strings

  15. 15

    Arrange eight consecutive number in a matrix so that no two number are adjacent

  16. 16

    CSS: Display only the first two letters of a string

  17. 17

    Reverse only letters in string and keep the same order of the words

  18. 18

    How to iterate letters in every string in the list at the same time?

  19. 19

    Random shuffling of String array

  20. 20

    RegEx: find every string with two or more letters

  21. 21

    Check if a string is build out of the same letters as another string

  22. 22

    Count number of same letters in a string

  23. 23

    How would I check to see if two strings contain the same letters

  24. 24

    Finding the number of matching letters in two different string at the same indices

  25. 25

    Check if string contains three same letters in Octave

  26. 26

    Remove words from file which has two or more of the same letters letters in a row

  27. 27

    Swap occurrences of two most frequent letters in a string

  28. 28

    Make two divs of variable width adjacent and centered at the same time

  29. 29

    Better way to swap two adjacent words in a PERL string?

HotTag

Archive