91. Decode Ways
Decode Ways (LeetCode 91) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
- Blind 75 · #54 of 75 · 1-D Dynamic Programming
- NeetCode 150 · #105 of 150 · 1-D Dynamic Programming
- NeetCode 250 · #181 of 250 · 1-D Dynamic Programming
How to solve Decode Ways
Decode Ways is a Fibonacci Style DP problem, part of Dynamic Programming (DP) Patterns. Solve problems with Fibonacci-like recurrence: climbing stairs, house robber, decode ways.
Work towards the solution in these steps:
- Consider using dynamic programming to keep track of the number of ways to decode the string up to each position.
- Think about how the decoding at each index depends on the previous one or two characters, similar to Fibonacci-style DP.
- Establish a DP array where each element represents the number of decode ways up to that index, initializing base cases carefully.
The reference solution runs in O(n) time and O(1) space.
The Decode Ways solution and editorial walks through the full approach with reference code.
Problem statement
You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:
"1" -> 'A'
"2" -> 'B'
...
"25" -> 'Y'
"26" -> 'Z'
However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25").
For example, "11106" can be decoded into:
- "AAJF" with the grouping (1, 1, 10, 6)
- "KJF" with the grouping (11, 10, 6)
- The grouping (1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid).
> Note: there may be strings that are impossible to decode.
Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: s = "12"
Output: 2
Explanation:
"12" could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: s = "226"
Output: 3
Explanation:
"226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Topics and companies
Topics: String, Dynamic Programming.
Reported in interviews at Adobe, Amazon, Apple, ByteDance, Cisco, Databricks, Facebook, Goldman Sachs, Google, JPMorgan, Lyft, Microsoft and others.
How to practise Decode Ways on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Decode Ways in the code editor, or read the Decode Ways editorial for a worked solution with its approach and complexity analysis.
Where Decode Ways sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 1D Array (Fibonacci Style) subpattern
- The full DSA patterns sheet
Problems related to Decode Ways
Other problems that use the same 1D Array (Fibonacci Style) and Dynamic Programming (DP) Patterns techniques:
- Climbing Stairs — Easy, same subpattern
- Delete and Earn — Medium, same subpattern
- Fibonacci Number — Easy, same subpattern
- House Robber — Medium, same subpattern
- House Robber II — Medium, same subpattern
- Integer Break — Medium, same subpattern
Decode Ways FAQ
What is the LeetCode number of Decode Ways?
Decode Ways is LeetCode problem 91, rated Medium.
What pattern is used to solve Decode Ways?
Decode Ways is a Fibonacci Style DP problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
What is the time complexity of Decode Ways?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Decode Ways?
Decode Ways has been reported in interviews at Adobe, Amazon, Apple, ByteDance, Cisco, Databricks, Facebook, Goldman Sachs, Google, JPMorgan, Lyft, Microsoft and others.