Decode Ways
Medium
String
Dynamic Programming
A message containing letters from `A-Z` can be encoded into numbers using the mapping `'A' -> "1", 'B' -> "2", ..., 'Z' -> "26"`. 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`.
Example 1
Input: s = "12"
Output: 2
Example 2
Input: s = "226"
Output: 3
Example 3
Input: s = "06"
Output: 0

Constraints

  • 1 <= s.length <= 100
  • s contains only digits and may contain leading zero(s).
Time Complexity
O(n)
Space Complexity
O(1)
14
Case 1
Input: "12"
Expected: 2
Case 2
Input: "226"
Expected: 3
Case 3
Input: "06"
Expected: 0