Momo, yes, there is a medical solution, it's called chemical castration.
With no sex drive, yes, they won't have inappropriate sexual thoughts.
In fact, you can receive chemical castration in multiple US states afaik, and in some states it is forced on you if you're a recidivist sex offender. But it has been criticized for being inhumane due to depriving the individuals in question of what is considered an essential human quality, and because of its inherent sexist nature (it being a prison alternative only available to males.) There's also the whole "what if you accidentally chemically castrate an innocent person? That would be cruel punishment" - and it being considered a cruel punishment by many.