Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /*
- QUESTION - https://www.youtube.com/watch?v=jFZmBQ569So&list=PL-Jc9J83PIiG8fE6rj9F5a6uyQ5WPdqKy&index=20 (COUNT ENCODINGS)
- */
- void getEncodings(string digits, string path, vector<char> &encoding){
- if(digits.empty()){
- cout << path << '\n';
- return;
- }
- // no encoding for '0', and no encoding for something like "03" as it is invalid
- if(digits[0] == '0'){
- return;
- }
- // level - encoding
- //options - digits to encode
- int oneChar = stoi(digits.substr(0, 1));
- getEncodings(digits.substr(1), path + encoding[oneChar], encoding);
- if(digits.size() >= 2){
- int twoChar = stoi(digits.substr(0, 2));
- if(twoChar > 0 && twoChar <= 26){
- getEncodings(digits.substr(2), path + encoding[twoChar], encoding);
- }
- }
- }
- int main() {
- // your code goes here
- string digits;
- cin >> digits;
- vector<char> encoding(27, -1);
- for(int i = 1; i < 27; i++){
- encoding[i] = (char) (i - 1) + 'a';
- }
- getEncodings(digits, "", encoding);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement