当前位置:首页|资讯

GFG Total Decoding Messages

作者:您是打尖儿还是住店呢发布时间:2024-10-05

Total Decoding Messages

A top secret message containing letters from A-Z is being encoded to numbers using the

following mapping:

'A' -> 1

'B' -> 2

...

'Z' -> 26

You are an FBI agent. You have to determine the total number of ways that message can be

decoded, as the answer can be large return the answer modulo 10 + 7.

Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string.

Example 1:

Input: str = "123"

Output: 3

Explanation: "123" can be decoded as "ABC"(123),

"LC"(12 3) and "AW"(1 23).

Example 2:

Input: str = "27"

Output: 1

Explanation: "27" can be decoded as "BG".

Your Task:

You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10 + 7.

----

全面解码信息

包含 A-Z 字母的绝密信息正被编码为数字,其映射如下

以下映射:

'A' -> 1

'B' -> 2

...

'Z' -> 26

你是一名联邦调查局特工。您必须确定该信息的解码方式总数。

因为答案可能很大,返回答案的模数 10 + 7。

注意:空数字序列被视为只有一种解码方式。如果有前导 0、多余的尾部 0 和两个或两个以上连续的 0,则为无效字符串。

例 1:

输入:str = "123”

输出: 3 3

说明 “123 “可以解码为 ”ABC"(123)、

“LC“(12 3) 和 ”AW"(1 23)。

例 2:

输入: str = "27

输出: 1 1

说明: “27 ”可以解码为 “BG”: “27 “可以解码为 ”BG"。

-----------

dp或者dfs,忘记讨论是0的情况,以及不可能存在的情况了,结果一直wa

这里i==1的情况,讨论了好几次,可以设置一个n+1的数组,然后dp[0]=1,就方便了。

这里补个dfs的超时的做法,至少我现在会dfs了。。。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1