A secret message is encoded as a string of digits, where each digit or pair of digits represents a letter according to the following mapping:
'1' → 'A', '2' → 'B', ..., '26' → 'Z'
The message can be decoded in multiple ways depending on how the digits are grouped. For example, the string "226" can be interpreted as:
Given a string str
that contains only digits, find out how many ways it can be decoded. If the string cannot be decoded in any valid way, return 0
.
str: string
: String consisting only of digits to decodeInput: str = "225"Output: 3Explanation: '225' can be decoded as 'BY' (2 26), 'VE' (22 5), or 'BBE' (2 2 5).
Input: str = "10"Output: 1Explanation: '10' can be decoded as 'J' (10).
Input: str = "1106"Output: 1Explanation: '1106' can be decoded as 'AJF' (1 10 6). The grouping (11 06) is invalid because '06' is not a valid code.
str.length
<= 100A secret message is encoded as a string of digits, where each digit or pair of digits represents a letter according to the following mapping:
'1' → 'A', '2' → 'B', ..., '26' → 'Z'
The message can be decoded in multiple ways depending on how the digits are grouped. For example, the string "226" can be interpreted as:
Given a string str
that contains only digits, find out how many ways it can be decoded. If the string cannot be decoded in any valid way, return 0
.
str: string
: String consisting only of digits to decodeInput: str = "225"Output: 3Explanation: '225' can be decoded as 'BY' (2 26), 'VE' (22 5), or 'BBE' (2 2 5).
Input: str = "10"Output: 1Explanation: '10' can be decoded as 'J' (10).
Input: str = "1106"Output: 1Explanation: '1106' can be decoded as 'AJF' (1 10 6). The grouping (11 06) is invalid because '06' is not a valid code.
str.length
<= 100console.log()
statements will appear here.