Write a recursive function named isElementSpellable
that uses backtracking to check whether a given string can be spelled out using just element symbols from the Periodic Table of the Elements.
For example, the word "began" can be spelled out as BeGaN (beryllium, gallium, nitrogen), and the word "feline" can be spelled out as FeLiNe (iron, lithium, neon).
Not all words have this property, though; the word "interesting" cannot be made out of element letters, nor can the word "chemistry" (though, interestingly, the word "physics" can be made as PHYSICS (phosphorous, hydrogen, yttrium, sulfur, iodine carbon, sulfur).
You don't need to know anything about chemistry or have the periodic table memorized to solve this problem; you are given a set
of strings containing all the element symbols in the periodic table.
Your function should accept two parameters:
a string
of the text to try to spell out, and a reference to a set
of all the element symbols in the Periodic Table.
Your function should return true
if that string can be written using only element symbols, and false
if it cannot.
If you like, you may assume the fact that all element symbols are at most three letters.
If passed the empty string, you should return true
.
You may use a loop in your solution if you like, but the overall algorithm must use recursion and backtracking.
Constraints:
Your function should not modify the state of the set passed in.