Statement:
Given an array of strings S. You can make a string by combining elements from the array S (you can use an element more than once)
In some situations, there are many ways to make a certain string from the array S.
Example:
S = {a, ab, ba}
Then there are 2 ways to make the string "aba":
- "a" + "ba"
- "ab" + "a"
Question:
Given an array of string S. Find the shortest string such that there are more that one way to make that string from S. If there're none, print out -1.
P/S: I have been thinking for many days but this is the best one I've got so far:
- Generate all permutations of the array
- For each permutation, make a string from the array S
- Check if that string is made before, if yes, print out the string, if not, save that string.
But this algorithm clearly won't pass all the test cases. I can't think of any better algorithm.