Problem 51
Element Spellable Recursion Challenge
Given a word and an array of element symbols, determine whether the word can be spelled by concatenating symbols from the list. Matching is case-insensitive, but the returned symbols must preserve the capitalization from the symbols array. Each symbol may be used more than once.
Function Signature
elementSpellable(text, symbols)
Parameters
text— a string representing the word to spell.symbols— an array of strings representing available element symbols (e.g.['Be', 'Ga', 'N']).
Output
Return an array of symbols (with original capitalization) that spell the word, or null if the word cannot be spelled. An empty string should return [].
Constraints
- The function must use recursion.
- Matching is case-insensitive.
- Returned symbols must preserve capitalization from the input array.
- The function should accept exactly two arguments.
Examples
elementSpellable('began', ['Be', 'Ga', 'N']) → ['Be', 'Ga', 'N']
elementSpellable('feline', ['Fe', 'Li', 'Ne']) → ['Fe', 'Li', 'Ne']
elementSpellable('BeGaN', ['Be', 'Ga', 'N']) → ['Be', 'Ga', 'N']
elementSpellable('interesting', ['I', 'N', 'Te']) → null
elementSpellable('', ['H', 'He']) → []
elementSpellable('began', []) → null
Edge Cases
- An empty string is always spellable and returns
[]. - An empty symbols array with a non-empty word returns
null. - Some words require backtracking when a greedy match leads to a dead end.