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).
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.
Task
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.
contains
builtin. \$\endgroup\$COC(C1)CCCC1C#N
can't be pasted to the tool you've linked (it automatically changes toCOC1CC(CCC1)C#N
..) Also, wouldCOC(C1)CCCCC1#N
withCCCCCC
result in truthy, since it does contain a substructure of six subsequenceC
-atoms (the entire circle(C1)CCCCC1
, and the additional branch to aC
inCOC
)? \$\endgroup\$COC(C1)CCCCC1#N
withCCCCCC
will be truthy.COC(C1)CCCCC1#N
withCOC1CC(CCC1)C#N
will be truthy. \$\endgroup\$