I recently came across this problem in the LC weekly, and came up with a dp+hashing solution. However, most accepted users, as well as the the intended solution, use a trie and iterates through the string n times. I discarded any N^2 ideas because I thought the constraint of 5e4 would be too large for N^2. Do you believe this is a result of weak testcases or is it actually reasonable to consider N^2 solutions with the constraints given? I always thought the rule of thumb for N^2 was 1e4, and believe these solutions would not survive if there was a hacking phase.
Problem: https://leetcode.com/problems/construct-string-with-minimum-cost/
Edit: As one user pointed out, the input: "aaa..." (length 50k) ["aaa..." (length 25k), "a"] [1,1], results in N^2 complexity in the worst case and would likely TLE most if not all of the trie DP solutions.
Well, if you carefully look at the constraints, its given sum of lengths of all strings in words<=5e4. The time complexity of the trie + dp solution is indeed equal to the sum of length of all strings, as each string in words is traversed at most once.
It is not that simple as there are N DP states and each state has a loop that goes at most N times. So the complexity is in the realm of N^2. Though as I think about the problem more, I believe due to the nature of the problem its pruning is relatively efficient. I'm still not entirely convinced that a trie DP solution could not be hacked though.
Could you send a sample solution...mine seems like O(sum of length of all strings) to me
Try the test in this comment:
https://leetcode.com/discuss/general-discussion/5431651/Weekly-Contest-405/2498825
Trie-DP solutions fail it, like this one https://leetcode.com/problems/construct-string-with-minimum-cost/discuss/5431976/C%2B%2B-oror-EASY-BEATS-100
Well...Lol..it fails my solution too...Okay, so beside the inability to deal with cheaters, LeetCode has lost the ability to set problems too...