It's rare for struggling retailers to come back from the brink, but that's exactly what has happened at Rite Aid (RAD -20.53%). Shares in the east and west coast pharmacy operator traded below $1 as mounting losses threatened its survival just a few years ago.

However, a significant restructuring that included refinancing debt and shuttering under-performing stores has returned the company to profitability. With Rite Aid reporting yet another quarter of earnings and margin growth, here are three reasons investors are increasingly optimistic that Rite Aid can compete against CVS (CVS -1.62%) and Walgreen (WBA -0.93%).

RAD Chart

RAD data by YCharts.

1. Quality over quantity
Rite Aid's struggles stem from its multibillion-dollar acquisition of Eckerd in 2007. At the time, Rite Aid hailed the deal as a game changer that would help it challenge market leaders CVS and Walgreen. The company believed the acquisition, which added 1,500 stores to Rite Aid's footprint, would become accretive to earnings within a year.

However, that never happened. Instead, Rite Aid floundered under an increasing debt load while newly acquired stores, many of which operated in the same markets as Rite Aid's existing locations, cannibalized revenue. To make matters worse, while Rite Aid struggled to overcome its headaches, CVS and Walgreen continued to expand throughout the pharmacy supply chain.

That prompted Rite Aid's management to shift its focus away from absolute sales growth to profit growth instead. As a result, Rite Aid has jettisoned more than 4% of its stores over the past four years, relocated more than 100 stores to better locations, and remodeled more than 1,000 stores in a bid to increase operating margin.

Rite Aid's focus on quality over quantity has led to surging profitability. It's gone from losing hundreds of millions a year to forecasting it will earn between $313 million and $423 million in the current fiscal 2015.

2. Boomers driving generic scripts
At the same time Rite Aid was putting money back into its best stores, the patent cliff was providing profit tailwinds. While low-priced generics weigh down pharmacies' top-line sales, their friendlier margins support earnings.

In 2013, some $28 billion in branded drugs lost patent exclusivity, but that pales in comparison to the $55 billion that saw patents expire in 2012. This year, the patent cliff re-exerts from its 2013 lull with an expected $34 billion in branded drugs losing patent protection. And pharmacies will enjoy even better margin next year when $66 billion in patents expire.

That generic tailwind has already helped Rite Aid's profit jump. In the company's fiscal fourth-quarter conference call this week, the company cited generic purchasing efficiency as a key reason behind its EBITDA margin expanding from 5.27% to 5.40% year over year last quarter. For comparison, Rite Aid's EBITDA margin was just 3.6% in fiscal 2012.

RAD EBITDA (TTM) Chart

RAD EBITDA (TTM) data by YCharts.

In addition to a margin-friendly patent calendar, aging consumers are also supporting Rite Aid's recovery. The average pharmacy customer over age 65 fills more than 28 prescriptions a year, while those under age 45 fill less than 10. Given more than 8,000 baby boomers are turning 65 every day, prescription volume should continue to provide a big tailwind for Rite Aid, CVS, and Walgreen.

3. Reforming the role of pharmacies
Boomers present a significant driver of Rite Aid's future growth, but Obamacare may have a more immediate impact. More than 3 million people are newly covered by Medicaid, and that population has historically been more inclined to visit emergency rooms and health care clinics than primary care doctors.

As a result, there's an opportunity for retailers like CVS and Walgreen, which already offer basic primary care services through clinics at hundreds of their stores to capture relationships with newly insured patients.

While Rite Aid has been slow to adopt a primary care model, it has had considerable success in providing flu shots and immunizations. The number of flu shots given at Rite Aid stores climbed from 675,000 in fiscal 2011 to more than 2.5 million last year. Demand for those shots should head even higher given millions of new customers will now be covered by insurance.

Additionally, Rite Aid's acquisition of Texas based MediClinic, which provides in-store clinics at 30 grocery stores in Texas, provides Rite Aid with a model it can -- over time -- roll out nationally. If it does, it could eventually erase CVS and Walgreen's lead in store-based clinics.

Fool-worthy final thoughts
Rite Aid will likely have bumps along the way, but it appears that after years of struggle, the company is back on solid ground. The company expects its existing stores will see their sales climb between 2.5% to 4.5% this year. If so, full-year sales should eclipse $26 billion for the first time in since 2012.