code: private static String getSubgraphSmiles(IAtomContainer subgraph, IAtomContainer molecule) throws CDKException {
          Set<IBond> bonds = new HashSet<>();
          for (IBond bond : subgraph.bonds())
              bonds.add(bond);
  
          Integer[] hCount = new Integer[subgraph.getAtomCount()];
          for (int i = 0; i < subgraph.getAtomCount(); i++) {
              final IAtom atom = subgraph.getAtom(i);
              int removed = 0;
              for (IBond bond : molecule.getConnectedBondsList(atom)) {
                  if (!bonds.contains(bond))
                      removed += bond.getOrder().numeric();
              }
              hCount[i] = atom.getImplicitHydrogenCount();
              atom.setImplicitHydrogenCount(hCount[i] == null ? removed : hCount[i] + removed);
          }
  
          String smi = cansmi(subgraph);
  
          // reset for fused rings!
          for (int i = 0; i < subgraph.getAtomCount(); i++) {
              subgraph.getAtom(i).setImplicitHydrogenCount(hCount[i]);
          }
  
          return smi;
      }
docs: New SMILES code respects atom valency hence a ring subgraph of 'o1cccc1CCCC' is correctly
  written as 'o1ccc[c]1' note there is no hydrogen there since it was an external attachment.
  To get unique subgraph SMILES we need to adjust valencies of atoms by adding Hydrogens. We
  base this on the sum of bond orders removed.
  
  @param subgraph subgraph (atom and bond refs in 'molecule')
  @param molecule the molecule
  @return the canonical smiles of the subgraph
  @throws CDKException something went wrong with SMILES gen