Skip to content

Commit

Permalink
Create Total_Decoding_Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenrishi authored Jan 27, 2023
1 parent eb2f45b commit 77030dc
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Day24/Total_Decoding_Messages
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends



class Solution {
public:
const int N=1000000007;
int countWaysUtil(string s, int i, vector<int>&dp)
{
if(i<=0)return 1;
if(dp[i]!=-1)return dp[i];
int ways=0;
if(s[i]>='1')
{
ways=countWaysUtil(s,i-1,dp);
}
if(s[i-1]=='1' or (s[i-1]=='2' and s[i]<'7'))
{
ways+=countWaysUtil(s,i-2,dp);
}
return dp[i]=ways%N;
}
int CountWays(string str){
// Code here
int n=str.size();
vector<int>dp(n,-1);

return countWaysUtil(str,n-1,dp)%N;
}

};


//{ Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
string str;
cin >> str;
Solution obj;
int ans = obj.CountWays(str);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends

0 comments on commit 77030dc

Please sign in to comment.