Diet cannot prevent ingested hair from accumulating, but it does influence how quickly hair moves through the digestive tract: the faster and better-lubricated the transit, the less time hair has to clump together into a ball in the stomach.
What actually happens in the stomach
A cat's tongue is covered with backward-pointing keratinous filiform papillae that act like a brush during grooming. Dead hairs are captured and swallowed, arriving in the stomach. In a healthy cat, most of these hairs follow normal transit and exit in the stools, invisible to the eye. It is only when they stagnate in the stomach, coil up, and compact with mucus that they form a trichobezoar, which may sometimes be vomited up.
This is where diet comes in: fibers (notably certain insoluble or fermentable fibers found in so-called "hairball control" diets) speed up peristalsis and increase the bulk of food, helping to move hair into the intestines before it clumps together. Good hydration, through wet food or constant access to water, also softens digestive contents and eases this transport. It is never a matter of "dissolving" the hair: no food digests keratin, and food acts only as a mechanical facilitator of transit.
A secondary tool, not the solution
Regular brushing remains the most effective way to reduce hairballs, because it removes dead hair directly before it is swallowed during grooming—diet only manages what has already been ingested. Repeated vomiting or regurgitation should not automatically be blamed on trichobezoars: they may signal another digestive condition. A veterinary examination is recommended if the problem becomes frequent.