LC 3800 - Minimum Cost to Make Two Binary Strings Equal

Approach This seems to be a greedy like problem, and I tried to walk the algorithm by-hand. For a simple case, cross and swap operations can be done by two flips. Therefore, if the cost for cross or swap is larger than two flips, we simply replace it with two flips cost. Then, we check when we need to use swap operation. Let’s say we have two sequence as follows s = 1 .. 0 t = 0 .. 1 If we want to eliminate the first difference by swapping, we need to find another (0, 1) pair to swap, since swapping with (0, 0) and (1, 1) did not change anything, this is just moving the difference backwards. So the greedy method is that we eliminate all the (1, 0) and (0, 1) pairs by swapping. ...

January 4, 2026 · 3 min

LC 3799 - Word Squares II

Approach This is like a backtracking problem, and we find that the size of the words is not large, so this should be the solution. We simply just try all the permutations in words[idx:end] (handling in traverse() function). This is done by swapping idx and i (from idx to end), and then call traverse() recursively. We can swap the idx and i back after the recursive call, in this case we can reuse the words order, and don’t need extra cache for the original words value (this method is commonly used in a backtracking problem). ...

January 4, 2026 · 2 min

LC 3798 - Largest Even Number

Approach We just simply check the last char to see if it is 0,2,4,6,8. We remove the character if it is not one of them, the final string then would be the answer. Code class Solution { public: string largestEven(string s) { while(s.size() > 0) { if((s[s.size()-1] - '0') % 2 == 0) break; s.pop_back(); } return s; } };

January 4, 2026 · 1 min