President Trump has spent much of his trip to Europe this past week lashing out at two of the most powerful women in the world.

The barrages aimed at British Prime Minister Theresa May and German Chancellor Angela Merkel prompted diplomats on both sides of the Atlantic to ask why a president who prizes his “great chemistry” with world leaders seems to go out of his way to damage two powerful female allies.

The two leaders are both in precarious positions politically at home, trying to hold together fragile coalitions and hold on to power. Trump’s critiques, followed by his over-the-top praise for them as friends, only complicate their positions.

The most recent attack came Thursday night when Trump told the Sun, a British newspaper, that May had ignored his advice on Britain’s exit from the European Union, placing a bilateral trade deal with the United States in jeopardy. The issue is especially fraught as May scrambles to maintain the coalition.

“I actually told Theresa May how to do it, but she didn’t agree,” Trump said of his advice to May on exiting the E.U. “She didn’t listen to me.” He then suggested that one of her main conservative rivals would make a “great prime minister.”

Standing next to May at a Friday news conference, Trump shifted his tone — describing May as an “incredible woman” doing a “fantastic job” and asserting that the British tabloid did not quote his more positive remarks about the prime minister.

May was careful not to praise or criticize Trump, who is highly unpopular in the U.K. But Trump filled the void. “I am doing a great job!” he said.

Trump’s tense relationship with Merkel and May is rooted in real policy disagreements on issues such as trade, defense spending and immigration, U.S. and European officials said.

But other analysts noted that the two female leaders had been less willing than others, such as French President Emmanuel Macron or Japanese Prime Minister Shinzo Abe, to cater to his ego. “I think it is much more about their approach to him,” said Danielle Pletka, senior vice president for foreign and defense policy studies at the conservative American Enterprise Institute. “Both of them have been confrontational toward him, and that’s not a good way to manage Donald Trump, in my opinion.”

Then there was a third explanation touted by several European diplomats over the past week: their gender. Trump has been particularly quick to praise self-styled strongmen, including North Korean leader Kim Jong Un, Philippine President Rodrigo Duterte and Russian President Vladi­mir Putin, whom he is scheduled to meet Monday in Helsinki.

“Everyone noted that Trump has been particularly harsh to the two female world leaders,” said one European official.

Added a second European official: “He has been more and more anti-E.U. He doesn’t care about allies. But he particularly has a problem with Merkel and May — who are women.”

The officials, like other former and current European and U.S. officials interviewed for this report, spoke on the condition of anonymity to describe candidly the president’s interactions with Merkel and May.

White House press secretary Sarah Huckabee Sanders said that Trump has a good relationship with both women. She noted that in Brussels, Trump gave Merkel “a huge hug, kissed both her cheeks and announced to everyone that he loves her.” 

His assessment of Germany on Friday was less enthusiastic: “It’s a very sad situation,” he said regarding the immigration policy that Merkel has championed.

During a news conference Friday with May and Thursday night over dinner at Blenheim Palace, Trump lavished the British leader with compliments. Shortly after he left the palace, Trump’s interview with the Sun landed. “May has wrecked Brexit . . . US deal is off,” the tabloid blared.

Before he arrived in London, Trump slammed Germany as “captive to Russia,” citing its reliance on Russian natural gas. His remarks drew a clipped rejoinder from the chancellor. “I myself experienced a part of Germany that was controlled by the Soviet Union,” she said. “I am very happy today that we are united in freedom as the Federal Republic of Germany.”

At the root of Trump’s testy relationship with May and Merkel is Trump’s growing anger over trade deficits.

“Trump is obsessed with trade and blames Merkel — incorrectly in my view — for the U.S.-E.U. trade deficit,” said Kristen Silverberg, a U.S. ambassador to the E.U. during the George W. Bush administration. 

Trump frequently complains to Merkel and to his aides about E.U. trade practices that favor Germany’s automobile industry at the expense of the United States.

His frustration with Merkel over the Russian natural gas issue also has been building for months. “Putin has got her by the balls with that gas pipe,” Trump complained earlier this year, according to two senior White House officials who heard the remark. At the time Trump was worried that Merkel might not follow through on a plan to expel Russian diplomats and suspected spies for the poisoning of a Russian ex-spy and his daughter on British soil.

One senior administration official said Trump often compliments Merkel as “a tough cookie.”

Meanwhile, Trump’s conversations with May are rarely convivial or easy. A former special adviser to May, who was present for calls and meetings with Trump, said the American president often began their discussions by encouraging Britain’s speedy withdrawal from the E.U.

“Have you left yet?” the president asked, according to the adviser. “When are you going to leave?”

Brexit is one of the most fraught political issues May faces, and her insistence on a gradual or “soft” withdrawal recently led to the resignations of two of her most important cabinet members — Boris Johnson, who was her foreign minister, and David Davis, who was her Brexit secretary.

Beyond those substantive disagreements, stylistic differences have contributed to Trump’s strains with Merkel and May. Trump rarely follows staff-
provided talking points in his conversations with world leaders. During May’s first lunch at the White House, he boasted of the large crowd of political supporters that would be coming to Washington that weekend for an antiabortion march. The comments unnerved several of May’s female advisers.

Trump has boasted to May about his “wonderful” golf courses and properties in the U.K. and complained about his relationship with Nicola Sturgeon, the Scottish first minister, who he said has treated him badly in business.

“It was like pinning jello to the wall,” the former May adviser said of the discursive talks.

Merkel, a German official said, tried to persuade Trump with detailed information, particularly on trade. Gaining little traction with the mercurial president, German officials have instead relied on conversations with others in Trump’s Cabinet.

Both women, though, have sought to achieve peace with Trump. May delivered a rare rebuke of Trump in November for retweeting an inflammatory, anti-immigrant video from a far-right U.K. political group. Since then she has told advisers that she will find a way to work with him — no matter what. 

“She would say, ‘He does things in an interesting way and you just have to get on with it,’ ” the ex-May adviser said.

James McAuley in Paris contributed to this report.