In an improving economy with low unemployment and steadily rising wages, CNBC's Jim Cramer wouldn't expect the dollar stores to be doing so well.
"In the last few months, both Dollar Tree and Dollar General's stocks have come roaring back. Dollar Tree's stock's up more than 40 percent from its summer lows. Dollar General's gaining nearly 27 percent. Not too shabby. Those are stunning moves, but they beg the question, how did the dollar stores get their groove back in a stronger economy?" Cramer said.
To understand their recent highs, Cramer backtracked to their lows. Starting in August 2016, shares of Dollar Tree, the parent company of Family Dollar, and Dollar General were taken down after a disappointing earnings report from Dollar General.
Overall, the results were worse than Wall Street expected, but the chain's same-store sales were particularly dismal, increasing by only 0.7 percent when analysts were expecting a 2.7 percent bump.
On Dollar General's conference call, management blamed the weather, food price deflation and food stamp cutbacks, as well as growing competition from Wal-Mart, for the underperformance.
Dollar Tree reported its earnings on the same day, also delivering weaker-than-expected same-store sales growth of 1.2 percent when the Street expected 2.3 percent. Its management said a weak economic environment was to blame, but also acknowledged the competition.
This was just the start of a series of bad earnings reports from the two companies, Cramer said. Food stamp cuts, food price deflation and lower prices continued to plague the chains, and worries that Dollar General's excess inventory would squeeze margins grew louder.
"In short, things were not looking good. However, it's important to point out that during this period of serial disappointments, both chains were indeed working hard to turn things around," the "Mad Money" host said.
Dollar Tree tried remodeling some of its stores, testing new products and broadening their health and beauty offerings. Dollar General also remodeled some locations, rolled out new product lines and started selling more fresh produce.
In the meantime, food price deflation slowed down and consumers started accounting for food stamp benefit decreases, erasing some of the headwinds the chains faced in months past.
By the time they reported their second-quarter results in August of 2017, things had clearly turned around, Cramer said.
Dollar Tree beat on the top and bottom lines, raised its full-year guidance and saw accelerating same-store sales growth of 2.4 percent. Dollar General missed slightly on earnings, but beat revenue estimates and saw 2.6 percent same-store sales growth.
"Some of this comeback simply has to do with an improving economy and a value-conscious consumer — the cheapness people learned during the  recession, that new frugality I talk about, won't be disappearing any time soon," Cramer said. "But a lot of it is because both Dollar Tree and Dollar General buckled down and did what they had to do to turn things around."
And while the stocks have run up wildly since their August lows — Dollar Tree jumping from $66 to over $93 and Dollar General running from $69 to over $83 as of Monday — Cramer said their stocks are still fairly cheap on a price-to-earnings basis.
"After struggling for the better part of the year, the dollar stores have gotten their groove back," the "Mad Money" host said. "Even though the stocks have run in recent months, I think Dollar Tree and Dollar General could have more upside, which is why they look so attractive when their stocks pull back like they did on this very day."