Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 91 Decode Ways - State Definition

Ways to decode

Define dp[i]dp[i] = number of ways to decode the substring from index ii to the end. Base case: dp[n]=1dp[n] = 1 (empty string = one valid decoding: do nothing). The answer is dp[0]dp[0], the ways to decode the entire string.

You could also define it as "ways to decode first ii characters" and build forward. Both work. Choosing the right state is often the hardest part. The state must capture everything needed to solve the subproblem independently.