Charmed hadrons from coalescence plus fragmentation in relativistic nucleus-nucleus collisions at RHIC and LHC