I came across an interesting Strings problem, but couldn't solve it. Suppose we are given N strings each of 3 characters, we need to construct a string of length N+2 such that it contains all of the above N strings as substrings. If the solution doesn't exist- Print "-1" Can anyone help me out in solving this?
-
You cannot guarantee the solution exists. – timrau Jan 27 '15 at 17:17
-
@timrau Yeah in that case We have to print "-1" – user3907480 Jan 27 '15 at 17:21
-
1Trivial example: "abc", "def", "ghi" "jkl", "mno", that's 5 strings but you won't be able to construct a 7 character long string that contains all of them as substring because there are 15 different characters there. – biziclop Jan 27 '15 at 17:21
-
1This interesting problem starts to look more and more like homework. – biziclop Jan 27 '15 at 17:22
-
@biziclop Yes I edited the problem and added this case. – user3907480 Jan 27 '15 at 17:22
-
@biziclop I am from US. Homework sucks at this place, let alone this type of problem. It was asked to a friend in an interview. I am applying for the same company. – user3907480 Jan 27 '15 at 17:23
-
What is the constraint of this problem? any limit for `N`? – Pham Trung Jan 27 '15 at 18:02
2 Answers
My answer is similar in nature to Pham Trung's one, but I choose to construct another graph for which the problem can be solved efficiently.
First, note that each of the given pieces has to appear in our result exactly once. There are
N
pieces andN
places where they can appear. When all the given pieces are different, this must be a bijection (one-to-one correspondence). When some of them are the same, the statement is not clear, but one can suspect we still have to place each piece exactly the number of times it occurs in the input.Now, construct the graph where each possible string of length 2 (one less than the size of a piece) is a vertex. For each piece αβγ in the input, construct an arc from vertex αβ to vertex βγ. Our task is now equivalent to finding an Eulerian path in this graph: a path of
N
arcs which traverses each given arc exactly once. This is a common problem with a simple polynomial solution: one depth-first search will do, see the above link for details.
First, we can see that the length of the final result is N+2, and each string has length 3 , which mean that, in the final result, there will be only one occurrence of each string, and, from string ith
to string (i +1)th
, they should share 2 characters.
So, we can create a directed graph, which N nodes represent N strings, and there is an edge between two nodes (a,b)
if last two character of node a
is the first two characters of node b
.
The problem is then reducing to finding the Hamiltonian path which is a NP-hard problem.
So, if number of N (less than 10) is small, we can just try every permutation of N strings, or using dynamic programming with bitmask
with N <= 17.
If the graph is directed acyclic graph (DAG), you can use topological sorting to solve it, and I believe you should ask the interviewer to clarify this.

- 1
- 1

- 11,204
- 2
- 24
- 43
-
I borrowed the general flow of your answer, but constructed a graph where the problem is reduced to finding an Eulerian path instead of Hamiltonian path. See my answer for details. – Gassa Jan 27 '15 at 18:57