The problem state that :
Given n binary strings, append to the end of each string a number of 0 or 1
so that no string is a prefix of another string and the number of operations is minimal
For example in this test case
1 10 11
The answer is 3 because you can change the strings into :
100 101 11
The problem constraint is n <= 100000 and the maximum string length is 1000000
There are 3 subtasks :
- Maximum string length is 1
- n <= 100 and maximum string length is 1000
- n <= 100000 and maximum string length is 1000000
I can solve the first testcase using dynamic programming
Basically I count the number of strings = 1 and number of strings = 0 (ones and zeros)
I have dp[i] = the least number of operations need to make i strings (that are 1 or 0) satisfy the condition
The formula is dp[i] = dp[i/2] + dp[i – i/2] + i and the answer will be dp[ones] + dp[zeros]
For example in this test case:
1 1 1 1 1
I change the strings into
11 11 11 10 10
It can be proven that the answer for these strings is dp[3] + dp[2]
I’m really struggling with this problem and I hope that someone can give me a way to solve it
Studio W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.