Element Symbol Spellings

Asked at GoogleOnsite

Reported by the problem's creator, not verified

A word can be spelled with chemical element symbols when it can be cut into consecutive pieces that are each a symbol. Matching ignores case, so "Physics" and "pHySiCs" are the same word and "Si" and "si" are the same symbol.

Implement count_spellings(symbols, word):

  • symbols is a list of element symbols, each 1 to 3 letters.
  • word is the word to spell.
  • Return the number of ways to cut word entirely into pieces that are symbols, or 0 when there is no way.

Two spellings are different when they cut the word at different places; the same symbol listed twice (in any case) does not create extra spellings.

Examples

  1. Example 1

    Input: symbols = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Nh","Fl","Mc","Lv","Ts","Og"], word = Physics

    Output: 4

    Explanation: The four spellings are P|H|Y|S|I|C|S, P|H|Y|Si|C|S, P|H|Y|S|I|Cs and P|H|Y|Si|Cs. The letters are compared without regard to case.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.