Limited Time Offer:Up to 20% off Hello Interview Premium
Up to 20% off Hello Interview Premium 🎉
Hello Interview
Learn Code
Introduction
Overview
Container With Most Water
Two Sum (Sorted Array)
3-Sum
Triangle Numbers
Move Zeroes
Sort Colors
Trapping Rain Water
Overview
Maximum Sum of Subarrays of Size K
Max Points You Can Obtain From Cards
Max Sum of Distinct Subarrays Length k
Overview
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Overview
Can Attend Meetings
Insert Interval
Non-Overlapping Intervals
Merge Intervals
Employee Free Time
Overview
Valid Parentheses
Decode String
Longest Valid Parentheses
Monotonic Stack
Daily Temperatures
Largest Rectangle in Histogram
Overview
Linked List Cycle
Palindrome Linked List
Remove Nth Node From End of List
Reorder List
Swap Nodes in Pairs
Overview
Apple Harvest (Koko Eating Bananas)
Search in Rotated Sorted Array
Split Array Largest Sum
Kth Smallest Element in a Sorted Matrix
Minimum Shipping Capacity
Overview
Kth Largest Element in an Array
K Closest Points to Origin
Find K Closest Elements
Merge K Sorted Lists
Median from Data Stream
Introduction
Fundamentals
Return Values
Maximum Depth of Binary Tree
Path Sum
Passing Values Down and Helper Functions
Validate Binary Search Tree
Calculate Tilt
Diameter of a Binary Tree
Path Sum II
Longest Univalue Path
Graphs Overview
Adjacency List
Copy Graph
Graph Valid Tree
Matrices
Flood Fill
Number of Islands
Surrounded Regions
Pacific Atlantic Water Flow
Introduction
Overview
Level Order Sum
Rightmost Node
Zigzag Level Order
Maximum Width of Binary Tree
Graphs Overview
Minimum Knight Moves
Rotting Oranges
01-Matrix
Bus Routes
Overview
Word Search
Solution Space Trees
Subsets
Generate Parentheses
Combination Sum
Palindrome Partitioning
N-Queens
Overview
Course Schedule
Course Schedule II
Shortest Path Algorithms
Network Delay Time
Cheapest Flights Within K Stops
Path With Minimum Effort
Find City with Fewest Reachable
Fundamentals
Solving a Question with Dynamic Programming
Counting Bits
Decode Ways
Unique Paths
Maximal Square
Longest Increasing Subsequence
Word Break
Maximum Profit in Job Scheduling
Paint House
Paint House II
Minimum Window Subsequence
Overview
Best Time to Buy and Sell Stock
Gas Station
Jump Game
Jump Game II
Partition Labels
Overview
Implement Trie Methods
Prefix Matching
Overview
Count Vowels in Substrings
Subarray Sum Equals K
Spiral Matrix
Rotate Image
Set Matrix Zeroes
Vote For New Content
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Dynamic Programming

Decode Ways

medium

max (21)341224132;341225102;21365487109
Count: 10
abcValid triangle requires:a + b > c AND a + c > b AND b + c > a(every pair must sum to more than the third side)3511SOURCE23211SOURCE23UNREACHABLE$100$100$100$5000SRC123DST$100$100$1000SRC123DST01233141Threshold: 4Answer: 32 reachable01234231118Threshold: 2Answer: 01 reachable1102233321432263321
DESCRIPTION (inspired by Leetcode.com)

Your are given a string s containing only digits. Write a function to return the number of ways to decode using the following mapping:

'1' -> "A"
'2' -> "B"
'3' -> "C"
...
'26' -> "Z"

There may be multiple ways to decode a string. For example, "14" can be decoded as "AD" or "N".

Input:

s = 101

Output:

1

Explanation: The only way to decode it is "JA". "01" cannot be decoded into "A" as "1" and "01" are different.

Explanation

This solution uses a bottom-up dynamic programming approach to solve the problem. We'll walkthrough how the solution solves the problem when the input string is s = 11106.
We create an integer array dp of length n + 1 where n is the length of the input string s. dp[i] is equal to the number of ways to decode the first i characters of the string s. If the first character of the string is 0, we can return 0 immediately. Otherwise, we initialize dp[0] = 1 (there is one way to decode an empty string) and dp[1] = 1 (one way to decode the first character of s).
Visualization
Python
Language
def num_decodings(s):
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
digit = int(s[i - 1])
if digit != 0:
dp[i] += dp[i - 1]
digit = int(s[i - 2:i])
if 10 <= digit <= 26:
dp[i] += dp[i - 2]
return dp[n]
11106dp012345

decode ways

0 / 1

We then use a for-loop i which goes from 2 to n to iterate through the string. The body of each loop determines the correct value of dp[i] by looking at the previous two characters of the string.

Single Digit

We first check the ith digit of the string. If it is not equal to 0, then the number of ways we to decode the first i characters of the string is equal to the number of ways to decode the first i - 1 characters of the string (each of those ways and the encoding of the ith digit). This allows us to set dp[i] = dp[i - 1].
Visualization
Python
Language
def num_decodings(s):
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
digit = int(s[i - 1])
if digit != 0:
dp[i] += dp[i - 1]
digit = int(s[i - 2:i])
if 10 <= digit <= 26:
dp[i] += dp[i - 2]
return dp[n]
i = 211106110000dp012345

i = 2

0 / 2

When it is equal to 0, it's not possible to decode the ith digit alone, so we leave dp[i] as 0 and continue.
Visualization
Python
Language
def num_decodings(s):
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
digit = int(s[i - 1])
if digit != 0:
dp[i] += dp[i - 1]
digit = int(s[i - 2:i])
if 10 <= digit <= 26:
dp[i] += dp[i - 2]
return dp[n]
i = 411106digit112300dp012345

digit = 0

0 / 1

Double Digit

Next, we move onto checking the ith and ith - 1 digits together. If they form a number between 10 and 26 (inclusive), then we can decode the ith and ith - 1 digits together. This means we have an additional dp[i - 2] ways to decode the first i characters of the string (each of those ways and the encoding of the ith and ith - 1 digits). This allows us to set dp[i] += dp[i - 2].
Visualization
Python
Language
def num_decodings(s):
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
digit = int(s[i - 1])
if digit != 0:
dp[i] += dp[i - 1]
digit = int(s[i - 2:i])
if 10 <= digit <= 26:
dp[i] += dp[i - 2]
return dp[n]
i = 211106digit111000dp012345

digit = 11

0 / 2

Finally, we return dp[n] which is the number of ways to decode the entire string.

Solution

Visualization
Python
Language
Try these examples:
def num_decodings(s):
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
digit = int(s[i - 1])
if digit != 0:
dp[i] += dp[i - 1]
digit = int(s[i - 2:i])
if 10 <= digit <= 26:
dp[i] += dp[i - 2]
return dp[n]
11106dp012345

decode ways

0 / 20

Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(n) where n is the length of the string. We iterate through the string once.

Space Complexity: O(n) where n is the length of the string. We use an array of length n + 1 to store the number of ways to decode the first i characters of the string.

Mark as read

Next: Unique Paths

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Guided Practice
Recent interview questions
Learn More
Reading Progress

On This Page

Explanation

Solution

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsAnthropic SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.

Login to track your progress