If you convert a chemical formula to lower case, it may become ambiguous: co can be both CO or Co.

Given an input consisting of a-z0-9, where 0 can only stand behind 0-9. Check whether it's ambiguous, unambiguous, or impossible. Return 3 different values for them. Shortest code wins.


co     - ambiguous
2h83o6 - unambiguous
li     - unambiguous
l      - impossible
cli    - ambiguous
c10h7o - unambiguous

One-sentence meaning: Is there zero, one or multiple ways to split input into concatenation of the following strings, case insensitive:



  • You need to code the table into your program, since as I searched there was no question requiring an unordered chemical element table.
  • \$\begingroup\$ Given an input consisting of a-z0-9 ~> So the input may contain j or q even if they're not used in any symbol, right? \$\endgroup\$
    – Arnauld
    Commented Jun 3, 2021 at 15:25
  • 3
    \$\begingroup\$ Most of the byte count is going to be that long string (or a variation thereof, possibly compressed), rather than the actual code. This makes the challenge less interesting, and is in fact an archetype of chameleon challenge. For similar challenges in the future, consider taking the string as an input \$\endgroup\$
    – Luis Mendo
    Commented Jun 3, 2021 at 16:49
  • \$\begingroup\$ Wait... how is 2h83o6 unambiguous in the test cases? Surely this could be decomposed to 2h8 3o6 or 2h 83o6 or 2h83 o6? \$\endgroup\$ Commented Jun 8, 2021 at 23:53
  • \$\begingroup\$ @KinuTheDragon What's 2H8 3O6? \$\endgroup\$
    – l4m2
    Commented Jun 9, 2021 at 2:38
  • \$\begingroup\$ @l4m2 I meant that 2h83o6 could possibly be reordered to have 2h8 3o6 in the chemical formula instead of the other forms. Basically, is a multi-digit number ambiguous? (e.g. 83 could be split 8 and 3) \$\endgroup\$ Commented Jun 9, 2021 at 10:45

4 Answers 4


JavaScript (ES6), 265 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or -1 for impossible.


Try it online!


The variable \$p\$ holds the final result. The flag \$b\$ is set whenever a 2-character chemical symbol is matched.

For each character \$c\$ at position \$i\$ in the input array:

  • If \$c\$ is a digit or the flag \$b\$ is set, we clear \$b\$ and leave \$p\$ unchanged.

  • Otherwise, we compute:

    p |= /E1/.test(c) + (b = /E2/.test(c + s[i + 1])) - 1

    where /E1/ and /E2/ are regular expressions matching 1-character and 2-character chemical symbols respectively.

    Which leads to:

     /E1/ matching | /E2/ matching | sum - 1 | meaning
     no            | no            |   -1    | impossible
     no            | yes           |    0    | unchanged
     yes           | no            |    0    | unchanged
     yes           | yes           |    1    | ambiguous
  • \$\begingroup\$ i[nr]|...|z[nr] -> [iz][nr] \$\endgroup\$
    – tsh
    Commented Jun 4, 2021 at 2:36
  • \$\begingroup\$ a[cglmr-u] ... c[adefl-orsu] ... e[rsu] -> a[cglmt] ... c[adefl-o] ... [ace][rsu] \$\endgroup\$
    – tsh
    Commented Jun 4, 2021 at 2:47
  • \$\begingroup\$ c[adefl-o] ... g[ade] ... n[abdehiop] -> c[fl-o] ... [cgn][ade] ... n[bhiop] \$\endgroup\$
    – tsh
    Commented Jun 4, 2021 at 2:49

Jelly, 194 bytes


Try it online!

-2 bytes thanks to caird coinheringaahing via better usage of chaining

Output 2 for ambiguous, 1 for unambiguous, 0 for impossible.

The logic behind this is pretty trivial, I just need to figure out a better way of encoding this periodic table...

“...’ṃØẠØD;µe€ØD;ØA¤œṗ  Helper Link; produce the list of components
“...’                   Compressed Integer
     ṃØẠ                Base-decoded into the upper and lower-case alphabet
        ØD;             Prepend the digits (as strings) as well
           µ            With the list of components (joined on nothing) as the left argument
            e€          Check if each element is in
              ØD;ØA¤    The digits + uppercase alphabet
                    œṗ  And partition the components before those indices

¢Œl⁸ŒṖ¤e€€ȦƇL«2         Main Link
¢Œl                     The components above, lowercased
   ⁸ŒṖ¤                 All ways of partitioning the input
         €              For each partition
        €               For each component
       e                Is it in the list of valid components?
          ȦƇ            Filter to keep partitions where every component was valid
            L           Length; 0 if impossible, 1 if unambiguous, 2+ if ambiguous
             «2         Minimum of the above and 2 (sets ambiguous case to 2 no matter how many occurrences existed)

05AB1E, 117 bytes

Prints -1 for impossible, 0 for unambiguous and 1 for ambiguous.


Try it online!

.œ              # push all partitions of the input
  ε      }      # map over the partitions:
   ...          #   push the table of valid parts (see below)
      s         #   swap to the current partition
       åP       #   test if every part is in the table
          O     # take the sum over all results
           <    # decrease by 1
            .±  # print the sign of this

Compressing the table

.•B...š•                  # alphabet compressed string (contains 26 spaces)
        #                 # split on spaces
         €S               # split each string into a list of chars
           ASõš           # the characters of the alphabet with the empty string prepended
               øε`ì}      # zip alphabet with previous list and prepend matching characters
                    ˜     # flatten the list
                     žhS« # append the digits

Try this part with step-by-step output!


Jelly, 124 bytes


Try it online!

A set of links that takes a string argument and returns 0 for impossible, 1 for a single valid interpretation and 2 for ambiguous.

Loosely based on @hyper-neutrino’s answer, but mostly rewritten.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.