• 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
  • 相关阅读:
    Linux网络抓包分析工具(tcpdump、wireshark)
    二手安捷伦E9323A射频传感器
    3D线扫相机中的深度数据与激光反射强度数据获取及其应用
    桃花峪滑雪场租赁系统 JAVA开源项目 毕业设计
    查找或替换excel换行符ctrl+j和word中的换行符^p,^l
    Spring:IOC与AOP
    一. Ubuntu入门
    软路由的负载均衡设置:优化网络性能和带宽利用率
    【AI】Python 实现粒子群算法
    关于 Rancher 与防火墙 firewalld 的一些注意事项
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126571707