Goldman Sachs | Phone Coderpad Interview | String Compression
Anonymous User
6373

Problem Description: Given a string replace the largest repeated substring at every point with an asterisk(*). The goal is end result should be a minimal length string after compression

For example, s = "abcabcd" should become "abc*d", Reason: we know abc has repeated twice, so replace the entire second instance of abc with an *.

and if s = "aabbaabb" it should become "a*bb*", Reason: At index 1, a is repeated twice so put an * there, and aabb has repeated twice so replace it's second instance with an *. In this example we don't put an * right after b at index 3 because aab* would represent aabaab, but that isn't the case.

Solution: The solution I came up with was at every even index check if the first half is equal to the second half, if it is, replace the entire second half with an *.

P.S: I made a mistake of misreading the question and spent a lot of time coding a wrong solution(came up with the optimal one by the end but couldn't code it up entirely). Don't repeat my mistake, and make sure you completely understand what's being asked :)

Comments (22)