Problem

Periodic Table Word Formations

GoogleFULLTIMEPHONE SCREEN
See Google hiring insights

You are given a single word. Determine in how many distinct ways the entire word can be formed by concatenating chemical element symbols from the periodic table, in order.

An element symbol is either a single letter (for example H, C, S, Y, I) or two letters (for example Si, Cs). Matching is case-insensitive: the letters of word are compared against element symbols ignoring case.

A valid formation is a sequence of element symbols whose concatenation, read left to right, exactly reproduces word. Two formations are different if they split the word at any different position or use any different symbol. Return the total count of valid formations.

Examples
01 · Example 1
word = "Physics"
return = 4

The four formations are P - H - Y - Si - C - S, P - H - Y - Si - Cs, P - H - Y - S - I - C - S, and P - H - Y - S - I - Cs.

Constraints

Constraints:

  • word consists only of alphabetic letters.
  • Matching between letters of word and element symbols is case-insensitive.
  • Each element symbol used must be a valid periodic table symbol of length 1 or 2.
  • The symbols of a formation, concatenated in order, must equal word with no leftover characters.
More Google problems
drafts saved locally
public int countWordFormations(String word) {
  // write your code here
}
word"Physics"
expected4
sign in to submit