• LeetCode每日一题(2266. Count Number of Texts)


    Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below.

    In order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the letter in the key.

    For example, to add the letter ‘s’, Alice has to press ‘7’ four times. Similarly, to add the letter ‘k’, Alice has to press ‘5’ twice.
    Note that the digits ‘0’ and ‘1’ do not map to any letters, so Alice does not use them.
    However, due to an error in transmission, Bob did not receive Alice’s text message but received a string of pressed keys instead.

    For example, when Alice sent the message “bob”, Bob received the string “2266622”.
    Given a string pressedKeys representing the string received by Bob, return the total number of possible text messages Alice could have sent.

    Since the answer may be very large, return it modulo 109 + 7.

    Example 1:

    Input: pressedKeys = “22233”
    Output: 8

    Explanation:
    The possible text messages Alice could have sent are:
    “aaadd”, “abdd”, “badd”, “cdd”, “aaae”, “abe”, “bae”, and “ce”.
    Since there are 8 possible messages, we return 8.
    Example 2:

    Input: pressedKeys = “222222222222222222222222222222222222”
    Output: 82876089

    Explanation:
    There are 2082876103 possible text messages Alice could have sent.
    Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) = 82876089.

    Constraints:

    • 1 <= pressedKeys.length <= 105
    • pressedKeys only consists of digits from ‘2’ - ‘9’.

    dp[i][0]为按 1 次 pressedKey[i]作为一个一个字母的数量
    dp[i][1]为按 2 次 pressedKey[i]作为一个一个字母的数量
    dp[i][2]为按 3 次 pressedKey[i]作为一个一个字母的数量
    dp[i][3]为按 4 次 pressedKey[i]作为一个一个字母的数量

    dp[i][0]因为跟前面的字母无关,所以应该直接等于 dp[i-1]所有的加和
    在 pressedKey[i] == pressedKey[i-1]情况下:
    dp[i][1] = dp[i-1][0]
    dp[i][2] = dp[i-1][1]
    dp[i][3] = dp[i-1][2]


    static M: i64 = 1000000007;
    
    impl Solution {
        pub fn count_texts(pressed_keys: String) -> i32 {
            let chars: Vec<char> = pressed_keys.chars().collect();
            let mut dp = vec![vec![0; 4]; pressed_keys.len()];
            dp[0][0] = 1;
            for i in 1..chars.len() {
                if chars[i] == chars[i - 1] {
                    dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3]) % M;
                    dp[i][1] = dp[i - 1][0];
                    dp[i][2] = dp[i - 1][1];
                    if chars[i] == '7' || chars[i] == '9' {
                        dp[i][3] = dp[i - 1][2];
                    }
                    continue;
                }
                dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3]) % M;
            }
            (dp.last().unwrap().into_iter().sum::<i64>() % M) as i32
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    pytorch的梯度图与autograd.grad和二阶求导
    如何在Mac上停止旋转等待光标?这里提供详细步骤
    太强啦!!!ChatGPT 能上传文件了,能执行 Python 代码啦!
    Etsy玩家必看之7个运营技巧
    centos8安装rabbitmq(rpm包)
    C++如何进行字符串分割,C++如何按照空格对字符串进行解析,C++如何按照逗号对字符串解析
    C Primer Plus(6) 中文版 第5章 运算符、表达式和语句 5.6 带参数的函数
    Linux二进制方式安装mysql8
    HTML期末学生大作业:中华传统文化【苏绣手工艺】带psd设计图(15页)
    Git基础
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126571707