Grocer Albertsons will buy drugstore chain Rite Aid in a deal that would accelerate the remaking of the U.S. retail and health-care industries.

The takeover serves several purposes. Rite Aid will get a buyer after a failed merger with another chain last year. Albertsons will add new locations and size amid increasing pressure from online competitors. And the grocer’s private-equity owners will exit their 2013 investment without having to go through an initial public offering in a turbulent market.

Rite Aid operates 15 stores in Maine, most in smaller communities roughly bounded by Waterville in the south, Millinocket in the north and Old Town in the east.

The Shaw’s supermarket chain, which has 21 stores in Maine, is owned by Albertsons.

The combined companies will have about 4,900 stores, including 4,350 pharmacy locations, in 38 states, they said in a statement. The Albertsons-owned pharmacies will be rebranded under the Rite Aid name, including those inside Shaw’s supermarkets.

Retailers have been under growing pressure from online competitors like Amazon.com, and the corner drugstore has been no exception. While the prescription drug businesses at pharmacies has been relatively stable, front-of-the-store sales have been in decline. Giant retailers like Walmart are also looking to play a bigger role.

The result has been consolidation. CVS Health Corp. agreed last year to pay about $68 billion for health insurer Aetna, while the Wall Street Journal recently reported that Walgreens Boots Alliance is in early talks to buy drug distributor AmerisourceBergen Corp. after its takeover of Rite Aid was scaled back last year for antitrust reasons.

Rite Aid shares rose 28 percent before U.S. markets opened on Tuesday. The companies said the deal is expected to close in the second half of the year.

Rite Aid shareholders will have a choice whether to take all stock or a combination of stock and cash. After it closes, Albertsons shareholders will own 70.4 percent to 72 percent of the business, the companies said.

The companies have a combined value of around $24 billion, including debt, according to the Wall Street Journal, which reported on the transaction earlier Tuesday.

The deal also comes after Rite Aid’s failed attempt to sell itself to Walgreens. That merger fell apart amid scrutiny by U.S. antitrust authorities. Walgreens eventually won approval to buy 1,932 stores, three distribution centers and related asset for $4.4 billion in September.

Rite Aid CEO John Standley will serve as chief executive officer of the new company, the companies said, while his counterpart at Albertsons, Bob Miller, will be the chairman. The companies haven’t decided on a name for the new company.

Albertsons, which is backed by private equity firm Cerberus Capital Management LP, last year put plans for an initial public offering on hold after Amazon acquired Whole Foods, according to people familiar with the situation.

Cerberus acquired Albertsons in a $3.3 billion deal with Supervalu in 2013. It later merged the business with Safeway, creating a grocery chain of 2,230 stores and 250,000 employees across the U.S.