Algorithm for largest word formed from perodic table elements

longest word made from the periodic table
all words that can be made with the periodic table
funny sentences made from the periodic table
periodic table of elements
periodic table insults
element words
swear words from the periodic table
christmas words made from the periodic table

I want to write an algorithm for the following problem scenario

Taking periodic table elements' names, find largest word that can be formed? The symbols such as Na , Ne etc should be regarded as single elements.

This was asked in a reputed company's job interview. Can anybody please help me out solve this.

Words You Can Make Using the Periodic Table, What is the longest word you can make from the periodic table? The periodic table symbol for nobelium is the word ‘No’. This is a list of words made from periodic table element symbols. Each element has a one- or two-letter symbol. An enormous number of words are possible, so if you have an addition to make to the list, feel free to post a reply.

I think a better way is to check every word in a dictionary and see if it can be made from the names of the elements. To check every permutation of the elements would be harder to program and would be less efficient.

While I agree it is easier to produce the combinations, there are just way too many of them, and as you said tend to infinity if you don't give a limit. The production of the word with the symbols would be slightly more difficult and finicky, but I don't think it would be too difficult.

E.g. when you get a word you could search through the elements looking for elements that could make up your word, then using that set of elements try and fill in the letters from start to finish. Obviously this gets harder with elements that aren't 2 letters and words that are odd in length.

You can actually use a sort of graph. Say you have 'silicon'. You can start with the letter 'S' or 'SI' which are both in the table. From there choose 'SI' because it is closer to your solution. If 'SI' does not lead to your solution you will have to come back and see if 'S' would work.

So it works as a kind of depth first search.

Longest Word Using Periodic Table Symbols, How many words can you make up using the elemental symbols of the periodic table? Chemical element symbols are one- and two-letter abbreviations for the element names. They are used to make the periodic table and chemical formulas easier to read. You can combine the symbols to make words. Here is a list of English words made from periodic table element symbols.

Generating all words and checking if they exist is impractical. There are 118^L words of length L, a too fast growing function. 1,643,032 three symbols words !

The other way round, as suggested by Makoto, is much more efficient. Try to reconstruct every word from a dictionary. There are on the order of 250,000 English words.

Checking a word is straightforward: see if any element symbol matches the start of the word, and continue with the remaining characters.

You must try all possible matches as a match could hide another. (I mean word ABC could be matched by symbol A and then be stuck because BC is not available, but it could be that AB and C are.) So the matching function will be recursive. I don't expect an exponential explosion in this matching process.

For maximum efficiency, you will store the symbols in a trie structure

Final hint: as you are just asked to find the longest match, try the words by decreasing lengths and stop at the first match.

Here is a simple solution in Python, without any optimization. Matching is done right to left, to allow printing the sequence of symbols in post-order:

Words= ['K', 'NaH', 'NaNaNaNa', 'HaKuNa']
Symbols= ['Na','K','H']

def Match(Word):
    # Match the end of the word with every symbol
    for S in Symbols:
        # In case of a partial match, recurse on the prefix
        if S == Word or (S == Word[-len(S):] and Match(Word[:-len(S)])):
            print S,
            return True

    # No match, report failure
    return False

for W in Words:
    if Match(W):

Na H
Na Na Na Na

Elemental Words, Can you name the longest word made using periodic table symbols for the chemical elements? Here is the answer. Algorithm for largest word formed from perodic table elements Hot Network Questions I need a title out of my name from a vehicle that my ex drive, refuses to cooperate, what can I do to resolve this?

Generate all possible words - naive approach.

Based on Generate all permutations of all lengths:

import itertools..
symbols = ['Na','K','H']
for i in range(len(symbols)):
 for word in itertools.permutations(symbols,i+1):
  print( ''.join(word) )

You can generate every possible combination, and check against a dictionary if its an acutal word. But it is inefficient and only for if you allow no repetitions of symbols.

Check if word can be built from symbols - still not perfect.

If you allow repetitions you need to check a word against the list of symbols. I propose the following:

import itertools..
words = ['K', 'NaH', 'NaNaNaNa', 'HaKuNa']
symbols = ['Na','K','H']
for i in range(len(symbols)):
 for word in itertools.permutations(symbols,i+1):
  print( ''.join(word) )

def can_be_built(word):
  pos = 0
  ret = True
  while(pos < len(word)):
   #following loop checks if word starting form `pos`
   #can be prefixed by a symbol
   symbol_match = False
   for symbol in symbols:
    if word[pos:pos+len(symbol)] == symbol:
      symbol_match = True
   if symbol_match == False:
     print('Word `{}` has no match for symbol from: {}'.format(word, word[pos:]))
     ret = False
   #if it does move forward
   pos += len(symbol)

  return ret

for word in words:
   print("{} can be built? {}".format(word, can_be_built(word)))

It iteratively check if a word prefix is a symbol, then moves forward until the end of word is reached.


K can be built? True
NaH can be built? True
NaNaNaNa can be built? True
Word `HaKuNa` has no match for symbol from: aKuNa
HaKuNa can be built? False

It still is not perfect.

Correct approach

As Makoto says, the prefix-check should return a list of every possible match. The algorithm should make a queue out of those matches, and check all possible paths. It would be kind of like building a graph of prefixes that match the word. And if one builds whole word you are home.

I think it is still fairly easy to correct my 2nd example, but I am out of time for the actual code. I think it's a good place for start.

Longest Word Using Periodic Table Symbols, Each element has a one- or two-letter symbol. An enormous number of words are possible, so if you have an addition to make to the list, feel free  from two or more elements chemically bonded. Atoms form molecules by bonding together. Atoms give, take, or share electrons to achieve full outer electron shells. Elements in the same group, or column, are similar because they typically have the same number of outer electrons. This table shows some easy-to-remember common numbers for each group.

List of Words Made from Periodic Table Element Symbols, Element symbols are abbreviations for elements on the periodic table. These are words made using the one- and two-letter chemical element #!python elements = "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

Words Made Using Periodic Table Element Symbols, So, as you probably know, each and every element in the Periodic Table has its own unique chemical symbol, which is either one, two, or three letters long. For  Fictional Elements are added to make all words possible. Enter your name, your school, science class, anything! Save a lo-res image for your own use. Order custom products or pre-made items with the links on each side.

The Longest Word Made from Chemical Symbols, I was hoping to see the longest possible word without element repetition. Scrabble but instead of tiles you have just one periodic table with which to make words. more verbose version of the no back-to-back repetitions algorithm I posted below: I made a python-Messenger bot that scrapes info. regarding COVID-19  A (partial but pretty extensive) list of words that can be made from the elements in the periodic table. - Words In The Periodic Table: Results

Using Python to find the longest word spellable with symbols from , Every periodic table element is either (1 letter or 2 letters), so I would hash all letter Now we start with the longest word in the dictionary and work our way down word can be made up of letter combinations from the periodic table since we Algorithm (6091) · google (1129) · Coding (988) · Data Structures (​860) · Java  A very simple algorithm is used to automatically select symbols: (bath becomes BaTh), add a '|' sign between symbols to force a break: (B|ath becomes BAtH). This is still a Beta version of the Periodic Table Writer, if you run into any issues or think of an additional feature let us know !

  • Is there a dictionary of words provided to check the output ?
  • I.N.C.O.N.S.P.I.C.U.O.U.S.N.Es.S
  • If you want to find the longest word, your first step would be to sort your dictionary from longest to shortest. Then try from the top and find the first one you can build with the symbols from the periodic table.
  • @Shahbaz Simply sorting the dictionary by length and stopping at the first match does not necessarily give the correct answer because multiletter elements count only as one symbol. Maybe the intention of the question is to see if you are trying to be to clever and optimize in a bug.
  • find largest word that can be formed is actually ambiguous. Do they need the largest combination of elements, or the largest resulting word?
  • And how do you propose to check if a word can be made from the symbols? It's quite vital to the question and answer. Also, it is very easy to program generating of combinations IMO.
  • Hmm... after thinking some more, you might be right, including repetition of symbols would make the possible words infinite. It might be easier to invent algorithms to check word against being possible to be built.
  • I figured out an algorithm that checks if a prefix of word is a symbol. But again, of course you are right. For each possible hit you should make a list (a queue), and try every possible path. I think it still is fairly easy to refactor and extend my proposal with this idea to be correct. I think the basic idea is fine.
  • I think that better wording for "kind of DFS" is building a graph of prefixes that match the word. And IMO it doesn't matter if worked like DFS or BFS.
  • You can use DP to check whether a word consists of only element names: Let f(i) be the maximum number of elements that can form the prefix w[1..i]. Then if there is an element string E of size k and we have w[i+1..i+k] = E, we have f(i + k) <= 1 + f(i)
  • Sorting the dictionary and stopping at the first match may give the wrong answer because multiletter elements count only as one symbol.