-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDecodingMessages.cpp
More file actions
59 lines (54 loc) · 913 Bytes
/
DecodingMessages.cpp
File metadata and controls
59 lines (54 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// A top secret message containing letters from A-Z is being encoded to numbers using the following mapping:
// 'A' -> 1
// 'B' -> 2
// ...
// 'Z' -> 26
// You have to determine the total number of ways that message can be decoded.
// Example:
// Input:
// 2
// 3
// 123
// 4
// 2563
// Output:
// 3
// 2
#include<bits/stdc++.h>
using namespace std;
int dec(string s,int n)
{
int dp[n+1],c=0;
dp[0]=1;
dp[1]=1;
if(s[0]=='0')
return 0;
for(int i=0;i<n-1;i++)
{
if(s[i]=='0'&&s[i+1]=='0')
return 0;
}
for(int i=2;i<n+1;i++)
{
dp[i]=0;
if(s[i-1]>'0')
dp[i]=dp[i-1];
if(s[i-2]=='1'||s[i-2]=='2'&&s[i-1]<'7')
dp[i]+=dp[i-2];
}
return dp[n];
}
int main() {
//code
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
cout<<dec(s,n)<<"\n";
}
return 0;
}