logo CodeStepByStep logo

isElementSpellable

Language/Type: Java recursion backtracking Lexicon Map
Related Links:

Write a recursive method 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 Lexicon containing all the element symbols in the periodic table. Your method should accept two parameters: a string of the text to try to spell out, and a Set of strings containing all the element symbols in the Periodic Table. Your method 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 method should not modify the state of the lexicon passed in.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.