Walgreens has finally figured out a way to buy Rite Aid -- or about half of it, at least.

The pharmacy giant said Tuesday that federal regulators have accepted its $4.4 billion deal to buy 1,932 Rite Aid stores. That will leave Rite Aid with about 2,600 stores once the deal is finished.

The purchase marks the end of a two-years-long fight for Walgreens, which initially tried to buy Rite Aid outright for $17 billion in October 2015.

The merger agreement faced intense scrutiny from the start, since it would have left the United States with just two major pharmacy chains: Walgreens and CVS.

The agreement was reworked several times over the past few years as Walgreens scaled back the number of stores it intended to purchase. In June, the company tried for a $5.2 billion deal that would have handed it 2,186 Rite Aid stores.

Walgreens recently revised its deal again, said company spokesman Michael Polzin. Polzin said that the waiting period for review by the Federal Trade Commission expired last night at midnight, effectively giving Walgreens the regulatory clearance it needs to move forward.

The Rite Aid pharmacies that Walgreens plans to buy will eventually be rebranded as Walgreens stores. But it won't happen all at once.

Polzin said the company will begin buying Rite Aids in October, adding that it expects to finish doing so in the spring of 2018.

Rite Aid stock was down nearly 7% in premarket trading Tuesday morning -- a change from Monday, when it rose as investors hoped for a better deal.

Walgreens stock, meanwhile, was up 2% Tuesday before open.

--CNNMoney David Goldman contributed to this story.