# Determine if a substructure is present in a chemical structure using SMILES string (substructure fingerprint)

A SMILES (Simplified molecular-input line-entry system) string is a string that represents a chemical structure using ASCII characters. For example, water ($$\H_2O\$$) can be written in SMILES as H-O-H.

However, for simplicity, the single bonds (-) and hydrogen atoms (H) are frequently omitted. Thus, a molecules with only single bonds like n-pentane ($$\CH_3CH_2CH_2CH_2CH_3\$$) can be represented as simply CCCCC, and ethanol ($$\CH_3CH_2OH\$$) as CCO or OCC (which atom you start from does not matter).

n-pentane:

ethanol:

In SMILES, double bonds are represented with = and triple bonds with #. So ethene:

can be represented as C=C, and hydrogen cyanide:

can be represented as C#N or N#C.

SMILES uses parentheses when representing branching:

Bromochlorodifluoromethane can be represented as FC(Br)(Cl)F, BrC(F)(F)Cl, C(F)(Cl)(F)Br, etc.

For rings, atoms that close rings are numbered:

First strip the H and start from any C. Going round the ring, we get CCCCCC. Since the first and last C are bonded, we write C1CCCCC1.

Use this tool: https://pubchem.ncbi.nlm.nih.gov/edit3/index.html to try drawing your own structures and convert them to SMILES, or vice versa.

Your program shall receive two SMILES string. The first one is a molecule, the second is a substructure (portion of a molecule). The program should return true if the substructure is found in the molecule and false if not. For simplicity, only above explanation of SMILES will be used (no need to consider stereochemistry like cis-trans, or aromaticity) and the only atoms will be:

• O
• C
• N
• F

Also, the substructure do not contain H.

Examples

CCCC C
true

CCCC CC
true

CCCC F
false

C1CCCCC1 CC
true

C1CCCCC1 C=C
false

COC(C1)CCCC1C#N C(C)(C)C // substructure is a C connected to 3 other Cs
true

COC(C1)CCCCC1#N COC1CC(CCC1)C#N // SMILES strings representing the same molecule
true

OC(CC1)CCC1CC(N)C(O)=O CCCCO
true

OC(CC1)CCC1CC(N)C(O)=O NCCO
true

OC(CC1)CCC1CC(N)C(O)=O COC
false


Shortest code wins. Refrain from using external libraries.

• Could you add a few more complexer test cases like the last one? The first five test cases can be solved using a single contains builtin. – Kevin Cruijssen Jun 12 '20 at 7:06
• As for that last test case, the COC(C1)CCCC1C#N can't be pasted to the tool you've linked (it automatically changes to COC1CC(CCC1)C#N..) Also, would COC(C1)CCCCC1#N with CCCCCC result in truthy, since it does contain a substructure of six subsequence C-atoms (the entire circle (C1)CCCCC1, and the additional branch to a C in COC)? – Kevin Cruijssen Jun 12 '20 at 7:07
• @KevinCruijssen i will add more examples. The tool converts the SMILES string to a canonical SMILES string (it uses a standard algorithm that ensures a unique output). COC(C1)CCCCC1#N with CCCCCC will be truthy. COC(C1)CCCCC1#N with COC1CC(CCC1)C#N will be truthy. – kuantumleap123 Jun 12 '20 at 7:17

MoleculeContainsQ@@Molecule/@#&

As you could guess, this checks if the first molecule (parsed via Molecule) contains the second one by using the MoleculeContainsQ function.