onnhyd / all_codes_with_bug

In this repository I will add all my code those I find correct but are not working somehow.
MIT License
2 stars 7 forks source link

91. Decode Ways #4

Open onnhyd opened 2 years ago

onnhyd commented 2 years ago

image

My Submission

class Solution {
public:
set<string>st;
        bool isValid(string curr)
        {
                if(curr[0] == '0')return false;
                if(curr.size() == 1)   
                {
                        if(curr[0] > '0' && curr[0]<='9')return true;
                }
                if(curr.size() == 2)
                {
                        if(curr > "00" && curr < "27")return true;
                }
                return false;
        }
void help(string prev, int p, int n, string s)
{
        if(p >= n)
        {
                st.insert(prev);                
        }
        string curr = "";
        curr += s[p];

        prev += s[p];

        if(isValid(curr))
        {
                help(prev+"-", p + 1, n, s);
        }

       if(p + 1 < n) 
       {
                curr += s[p + 1];
                prev += s[p + 1];//

                if(isValid(curr))
                {
                        help(prev+"-", p + 2, n, s);
                }

       }

}
    int numDecodings(string s) {
        int p = 0, n = s.size();
            string prev = "";
            help(prev, p, n, s);
            return st.size();
    }
};
onnhyd commented 2 years ago

ADD DP without Changing The Approach

Dhanunjay18 commented 2 years ago

I can do this using dp please assign it to me

Dhanunjay18 commented 2 years ago

I've resolved the issue can u plz check and update the label.