4

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?

user3907480
  • 440
  • 1
  • 6
  • 10

2 Answers2

4

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.

  1. First, note that each of the given pieces has to appear in our result exactly once. There are N pieces and N 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.

  2. 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.

Community
  • 1
  • 1
Gassa
  • 8,546
  • 3
  • 29
  • 49
3

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.

Community
  • 1
  • 1
Pham Trung
  • 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