class Solution {
public:
void dfs(string low, string high, string path, int len, int &res) {
if (path.size() == len) {
if (!(len != 1 && path[0] == '0') &&
!(len == low.size() && path.compare(low) < 0) &&
!(len == high.size() && path.compare(high) > 0)) {
++res;
}
return;
}
dfs(low, high, "0" + path + "0", len, res);
dfs(low, high, "1" + path + "1", len, res);
dfs(low, high, "6" + path + "9", len, res);
dfs(low, high, "8" + path + "8", len, res);
dfs(low, high, "9" + path + "6", len, res);
}
int strobogrammaticInRange(string low, string high) {
int res = 0;
for (int i = low.size(); i <= high.size(); ++i) {
dfs(low, high, "", i, res);
dfs(low, high, "0", i, res);
dfs(low, high, "1", i, res);
dfs(low, high, "8", i, res);
}
return res;
}
}