patternMinor
Rearranging strings so that the Hamming distance between them is 1
Viewed 0 times
therearrangingdistancebetweenthatthemstringshamming
Problem
This is a question from CodeFights.com:
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a way that after the rearrangement the strings at consecutive positions would differ by exactly one character.
Example
-
For
All rearrangements don't satisfy the description condition. (sic)
-
For
Strings can be rearranged in the following way:
I found an exponential-time algorithm, but is there a polynomial-time algorithm? Is this problem NP-complete?
I know that a closely related problem, the Hamming Salesman Problem (HTSP), is NP-complete, and this problem is certainly reducible to HTSP, but I don't know if HTSP is reducible to this one.
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a way that after the rearrangement the strings at consecutive positions would differ by exactly one character.
Example
-
For
inputArray = ["aba", "bbb", "bab"], the output should bestringsRearrangement(inputArray) = false;All rearrangements don't satisfy the description condition. (sic)
-
For
inputArray = ["ab", "bb", "aa"], the output should bestringsRearrangement(inputArray) = true.Strings can be rearranged in the following way:
"aa", "ab", "bb".I found an exponential-time algorithm, but is there a polynomial-time algorithm? Is this problem NP-complete?
I know that a closely related problem, the Hamming Salesman Problem (HTSP), is NP-complete, and this problem is certainly reducible to HTSP, but I don't know if HTSP is reducible to this one.
Solution
Grid graphs are finite node-induced graphs of the infinite planar grid (whose vertices are integral points, and two vertices are linked when their Euclidean distance is 1). A. Itai, C. H. Papadimitriou, and J. L. Szwarcfiter proved that the Hamiltonian path problem on grid graphs is NP-complete.
To reduce that to your problem, suppose the grid graph has $m$ rows and $n$ columns. The $i$-th row is labeled with the string $a^ib^{m-i}$, and the $j$-th column is labeled with the string $a^jb^{n-j}$. Then a vertex corresponds to the concatenation of the two labels of its row and column. Now it is easy to show the unit disk graph under Hamming distance over all these concatenated strings is exactly the same as the original grid graph.
To reduce that to your problem, suppose the grid graph has $m$ rows and $n$ columns. The $i$-th row is labeled with the string $a^ib^{m-i}$, and the $j$-th column is labeled with the string $a^jb^{n-j}$. Then a vertex corresponds to the concatenation of the two labels of its row and column. Now it is easy to show the unit disk graph under Hamming distance over all these concatenated strings is exactly the same as the original grid graph.
Context
StackExchange Computer Science Q#82621, answer score: 4
Revisions (0)
No revisions yet.