Advertisement
Fastrail08

Count Encodings

Jun 4th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5. QUESTION - https://www.youtube.com/watch?v=jFZmBQ569So&list=PL-Jc9J83PIiG8fE6rj9F5a6uyQ5WPdqKy&index=20 (COUNT ENCODINGS)
  6. */
  7.  
  8. void getEncodings(string digits, string path, vector<char> &encoding){
  9.     if(digits.empty()){
  10.         cout << path << '\n';
  11.         return;
  12.     }
  13.     // no encoding for '0', and no encoding for something like "03" as it is invalid
  14.     if(digits[0] == '0'){
  15.         return;
  16.     }
  17.     // level - encoding
  18.     //options - digits to encode
  19.     int oneChar = stoi(digits.substr(0, 1));
  20.     getEncodings(digits.substr(1), path + encoding[oneChar], encoding);
  21.    
  22.     if(digits.size() >= 2){
  23.         int twoChar = stoi(digits.substr(0, 2));
  24.         if(twoChar > 0 && twoChar <= 26){
  25.             getEncodings(digits.substr(2), path + encoding[twoChar], encoding);
  26.         }
  27.     }
  28. }
  29.  
  30.  
  31.  
  32. int main() {
  33.     // your code goes here
  34.     string digits;
  35.     cin >> digits;
  36.     vector<char> encoding(27, -1);
  37.     for(int i = 1; i < 27; i++){
  38.         encoding[i] = (char) (i - 1) + 'a';
  39.     }
  40.     getEncodings(digits, "", encoding);
  41.  
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement