Update: I was wrong. It's kind of scary how sure I was of my incorrect solution.
Update: I was wrong. It's kind of scary how sure I was of my incorrect solution.
I've thought "How can my solution possible be wrong" numerous times while doing those problems. Makes you realize how often mistakes slip through in real code, 'that can't possibly be wrong'. Even when you have tests, because for complex behavior, your test case solution may easily be wrong (or worse: derived from your algorithm... I'm guilty of that mistake)
It is a very standard problem, by the way. Most books on algorithms will have the solution.
What would you call this algorithm?
I solved the second problem, but my solution is brute-force, and I'd like to see a better way to do it.
Gur grpuavdhr vf pnyyrq "qlanzvp cebtenzzvat". Naq, guvf cnegvphyne ceboyrz vf pnyyrq gur "pbva punatr ceboyrz".