feat: added alternative solution and TLE solution to decode-ways problem, wrote the tutorial file and added some more manual test cases
This commit is contained in:
40
decode-ways/src/TLE.cpp
Normal file
40
decode-ways/src/TLE.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
int ways(string &s, int n, int idx = 0)
|
||||
{
|
||||
if (idx > n)
|
||||
return 1;
|
||||
if (s[idx] == '0')
|
||||
return 0;
|
||||
|
||||
int count = ways(s, n, idx + 1);
|
||||
if (idx + 1 < n)
|
||||
{
|
||||
int num = stoi(s.substr(idx, 2));
|
||||
if (num >= 10 && num <= 26)
|
||||
count += ways(s, n, idx + 2);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
vector<bool> used;
|
||||
int numDecodings(string &s)
|
||||
{
|
||||
int n = (int)s.size();
|
||||
used.resize(n, false);
|
||||
return ways(s, n);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string code;
|
||||
cin >> code;
|
||||
cout << numDecodings(code) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user