My son’s been kinda obsessed, recently, with what words can be spelled using element abbreviations. I suggested writing a program that could check whether any given string could be spelled, and then had to immediately try it myself because I wasn’t sure, at first, how to do it. The main trick is in handling the possibility that using a one-letter element abbreviation where a two-letter element abbreviation is also possible could make an otherwise possible word look impossible (
frog, for example).
I was able to come up with a solution using recursion that was pretty fun. Click “result” in the CodePen below if you want to see the code in action.
See the Pen Spell with Elements by Nate Eagle (@neagle) on CodePen.