logo CodeStepByStep logo

isElementSpellable

Language/Type: C++ recursion backtracking Lexicon Map
Related Links:
Author: Marty Stepp (on 2016/06/16)

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 Lexicon 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 Lexicon 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 lexicon passed in.

Type your C++ solution code here:


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.