Straight forward C++ solution


  • 0
    S
    vector<string> findStrobogrammatic(int n) {
            string s;
            helper(n,0,s);
            return res;
        }
        void helper(int n , int i, string &s) {
            vector<string> cand = {"0","1","8","6","9"};
            if(i == n) {
                res.push_back(s);
                return;
            } 
            if(i == 0) {
                int j = 1;
                int k = 5;
                if(n == 1) {
                    j = 0;
                    k = 3;
                }
                for(; j < k; j++) {
                    helper(n,i+1,s+=cand[j]);
                    s.pop_back();
                }
            }
            else if(i < n/2) {
                for(int j = 0; j < cand.size(); j++) {
                    helper(n,i+1,s+=cand[j]);
                    s.pop_back();
                }
            }
            else if(n%2 != 0 && i == n/2) {
                for(int j =0 ; j<3; j++) {
                    helper(n,i+1,s+=cand[j]);
                    s.pop_back();
                }
            }
            else {
                helper(n,i+1,s+=mp[s[n-i-1]]);
                s.pop_back();
            }
        }
    private:
        vector<string> cand = {"0","1","8","6","9"};
        vector<string> res;
        unordered_map<char,char> mp={{'0','0'}, {'1','1'}, {'8','8'},{'6','9'},{'9','6'}};
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.