filmov
tv
Total Ways To Decode A String - Recursive Dynamic Programming Approach ('Decode Ways' on LeetCode)

Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - 1 'B' - 2 ... 'Z' - 26. Given a non-empty string containing only digits, determine the total number of ways to decode it.
Examples:
1
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
2
Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Complexities:
n is the total digits in the input string
Time: O( n )
Memoization prunes our recursion tree and we will do a linear amount of work to solve the problem.
Space: O( n )
We will need to store the answer to up to n subproblems that we will need to calculate
++++++++++++++++++++++++++++++++++++++++++++++++++
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - 1 'B' - 2 ... 'Z' - 26. Given a non-empty string containing only digits, determine the total number of ways to decode it.
Examples:
1
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
2
Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Complexities:
n is the total digits in the input string
Time: O( n )
Memoization prunes our recursion tree and we will do a linear amount of work to solve the problem.
Space: O( n )
We will need to store the answer to up to n subproblems that we will need to calculate
++++++++++++++++++++++++++++++++++++++++++++++++++
Комментарии