The decoding method of LeetCode dynamic programming

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

topic

Decoding Method A message containing the letters AZ is encoded with the following mapping:

'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
复制代码

To decode the encoded message, all numbers must be mapped back to letters based on the method above (there may be multiple methods). For example, "11106" can be mapped as:

"AAJF" ,将消息分组为 (1 1 10 6)
"KJF" ,将消息分组为 (11 10 6)
复制代码

Note that messages cannot be grouped as (1 11 06) because "06" cannot be mapped to "F" because "6" and "06" are not equivalent in the mapping.

Given a non-empty string s containing only numbers, count and return the total number of decoding methods.

The question data guarantees that the answer must be a 32-bit integer.

Example 1:

输入:s = "12"
输出:2
解释:它可以解码为 "AB"(1 2)或者 "L"(12)。
示例 2:

输入:s = "226"
输出:3
解释:它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
示例 3:

输入:s = "0"
输出:0
解释:没有字符映射到以 0 开头的数字。
含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。
由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。
 
复制代码

hint:

1 <= s.length <= 100
s 只包含数字,并且可能包含前导零。
复制代码

answer

problem-solving analysis

Problem- solving ideas
According to the meaning of the question, for a given string s, let its length be n, and the characters in it are from left to right s[1], s[2],⋯,s[n]. The methods we can use 动态规划to calculate the number of decoding methods for the string s.

Specifically, let fi denote the number s[1..i]of decoding methods for the first i characters of the . When making a state transition, we can consider which characters in s were used in the last decoding, then there are two situations:

Case 1: We use a character, namely s[i]to decode, so as long as s[i]is not equal to 0, it can be decoded into a letter in A∼I. Since the i-1number characters is fi -1, we can write the state transition equation:

fi = fi -1, 其中 s[i] 不等于 0
复制代码

Case 2: We use two characters, s[i−1]and s[i]for encoding. Similar to the first case, s[i-1]cannot be equal to 0, s[i-1]and s[i]the integers of and must be less than or equal to 26, so that they can be decoded J∼Zas a letter in . Since the i-2number characters is fi-2, we can write the state transition equation:

fi =  fi -2, 其中 s[i - 1] 不等于 0 并且 10 * s[i-1] + s[i] <= 26
复制代码

It should be noted that the transfer can only be performed i>1when , otherwise s[i-1]does not exist.

The value can be obtained by accumulating the above two state transition equations when the corresponding conditions are satisfied. After the dynamic programming is completed, the final answer is fn

Notice:

The boundary conditions for dynamic programming are:

f0 = 1
复制代码

That is, an empty string can have 1 decoding method to decode an empty string.

Complexity Analysis

  • Time complexity: O(N)
  • Space complexity: O(N)

problem solving code

The solution code is as follows (detailed comments in the code):

class Solution {
    public int numDecodings(String s) {
int n = s.length();
            int[] f = new int[n + 1];
            f[0] = 1;
            for (int i = 1; i <= n; i++) {
                if (s.charAt(i - 1) != '0') {
                    f[i] += f[i - 1];
                }
                if (i > 1 && s.charAt(i - 2) != '0'
                        && (((s.charAt(i - 2)- '0') * 10 + (s.charAt(i - 1) - '0')) <= 26)) {
                    f[i] += f[i - 2];
                }
            }
            return f[n];
    }
}

复制代码

Feedback results after submission:

image.png

Reference Information

Guess you like

Origin juejin.im/post/7080574970820034574