Then, they rallied behind Ahn Hee-jung, a left-centrist provincial governor. But Mr. Ahn was eliminated this month when he lost the Democratic Party primary to Mr. Moon.

Ahn Cheol-soo’s support has since skyrocketed.

“For many centrist and conservative voters, there is no one else to turn to except Ahn Cheol-soo,” said Kim Jiyoon, a polling expert at the Asan Institute for Policy Studies in Seoul. “Whether he can win the election will depend largely on how many of the anti-Moon voters he can persuade to come out and vote for him.”

Mr. Moon and Mr. Ahn share remarkably similar views on many issues.

Both have liberal goals like narrowing income inequality and overhauling the chaebol, the family-controlled conglomerates like Samsung that have dominated the economy for decades, often through collusive ties with government, as shown in Ms. Park’s scandal.

Both promise to review the unpopular agreement Ms. Park’s government had struck with Japan on “comfort women,” the Korean women forced into sexual slavery by Japan during World War II.

And they both emphasize the importance of the alliance with the United States. But they argue that sanctions and pressure alone have failed to stop North Korea’s weapons program and that it is time to try dialogue. During a televised debate on Thursday, both opposed action by the United States that might prompt war on the peninsula, like a pre-emptive military strike against the North.

The candidates even grew up in the same town: Busan, a port city in the southeast.

But they have vastly different backgrounds and public images. Mr. Ahn is a paragon of elite success. A son of a medical doctor, Mr. Ahn quit practicing medicine in 1995 and built a fortune developing the country’s most successful antivirus software. He later became a graduate school dean at Seoul National University, his alma mater.

During the debate, he said he would use his school connections and business background to build a rapport with Mr. Trump. Both Mr. Ahn and Mr. Trump attended the Wharton School of the University of Pennsylvania.