Strobogrammatic Number III

hard recursion backtracking strings

Problem

Count strobogrammatic numbers in the inclusive range [low, high]. low and high are given as numeric strings.

Inputlow = "50", high = "100"
Output3
69, 88, 96 fall in [50, 100].

def strobogrammatic_in_range(low, high):
    pairs = [("0","0"),("1","1"),("6","9"),("8","8"),("9","6")]
    def gen(n, top):
        if n == 0: return [""]
        if n == 1: return ["0","1","8"]
        inner = gen(n - 2, False)
        out = []
        for a, b in pairs:
            if top and a == "0": continue
            for s in inner:
                out.append(a + s + b)
        return out
    count = 0
    for n in range(len(low), len(high) + 1):
        for s in gen(n, True):
            if (len(s) == len(low) and s < low) or (len(s) == len(high) and s > high):
                continue
            count += 1
    return count
function strobogrammaticInRange(low, high) {
  const pairs = [["0","0"],["1","1"],["6","9"],["8","8"],["9","6"]];
  function gen(n, top) {
    if (n === 0) return [""];
    if (n === 1) return ["0","1","8"];
    const inner = gen(n - 2, false);
    const out = [];
    for (const [a, b] of pairs) {
      if (top && a === "0") continue;
      for (const s of inner) out.push(a + s + b);
    }
    return out;
  }
  let count = 0;
  for (let n = low.length; n <= high.length; n++) {
    for (const s of gen(n, true)) {
      if ((s.length === low.length && s < low) || (s.length === high.length && s > high)) continue;
      count++;
    }
  }
  return count;
}
class Solution {
    String[][] pairs = {{"0","0"},{"1","1"},{"6","9"},{"8","8"},{"9","6"}};
    public int strobogrammaticInRange(String low, String high) {
        int count = 0;
        for (int n = low.length(); n <= high.length(); n++) {
            for (String s : gen(n, true)) {
                if ((s.length() == low.length() && s.compareTo(low) < 0) ||
                    (s.length() == high.length() && s.compareTo(high) > 0)) continue;
                count++;
            }
        }
        return count;
    }
    List gen(int n, boolean top) {
        if (n == 0) return List.of("");
        if (n == 1) return List.of("0","1","8");
        List inner = gen(n - 2, false), out = new ArrayList<>();
        for (String[] p : pairs) {
            if (top && p[0].equals("0")) continue;
            for (String s : inner) out.add(p[0] + s + p[1]);
        }
        return out;
    }
}
class Solution {
    vector> pairs = {{"0","0"},{"1","1"},{"6","9"},{"8","8"},{"9","6"}};
    vector gen(int n, bool top) {
        if (n == 0) return {""};
        if (n == 1) return {"0","1","8"};
        auto inner = gen(n - 2, false);
        vector out;
        for (auto& [a,b] : pairs) {
            if (top && a == "0") continue;
            for (auto& s : inner) out.push_back(a + s + b);
        }
        return out;
    }
public:
    int strobogrammaticInRange(string low, string high) {
        int count = 0;
        for (int n = low.size(); n <= (int)high.size(); n++)
            for (auto& s : gen(n, true)) {
                if ((s.size() == low.size() && s < low) ||
                    (s.size() == high.size() && s > high)) continue;
                count++;
            }
        return count;
    }
};
Time: O(5^(n/2) per length) Space: O(5^(n/2))