LC 2977 - Minimum Cost to Convert String II

Approach This is a really hardr problem, originally I was thinking that for some string like a.. and b.., we must find somethiong to replace the first character, but I did not notice that the problme describe that the changing substring cannot be overlap (it can only be seperate or the same range). I originally think that ace as source and bcd as target, we might have a cost like ce to ed and aed to bcd, then we can combine both to transit to the target. ...

January 30, 2026 · 3 min

LC 2976 - Minimum Cost to Convert String I

Approach This is likely a APSP (all pair shortest path) problem, and can be done with Floyd-Warshall algorithm. Originally I was using 4-loops to check if the dist matrix is converging, but actually the algorithm only need 3 loops with k,i,j. I also spend some time to review the floyd warshell algorithm. This is true due to the DP property. the original cost can be formed into a matrix dist_0, and when we construct dist_1, we iterate thorugh k=1 meaning that we allow 1 to be in the shortest path. When doing k=2, we allow 2,1 to be in the shortest path since we combine the solution from dist_1, and is currently looking into solution 2. ...

January 29, 2026 · 2 min