Count ways to decode a digit string into letters
A radio club sends short messages as digits only: each letter becomes its position in the alphabet (A is 1, B is 2, …, Z is 26), and the codes are written back to back with no separator. Without separators a transmission can be ambiguous: '25' could be 'BE' (2, 5) or 'Y' (25).
Implement countDecodings(code). It returns the number of different letter sequences that code can be decoded into.
countDecodings(code) // => number
code into consecutive parts of one or two digits. A split is valid when every part is a letter code from 1 to 26 written without a leading zero. So '0', '06' and '27' are not letter codes.0 when there is none (for example, when code starts with 0 or contains '30').0.code contains any character other than the digits 0–9, throw a TypeError.code can be up to 60 digits long. The answer then needs up to 13 digits, which still fits in a JavaScript number, but trying every split one by one is far too slow. Your solution must run in linear time.countDecodings('25'); // => 2 (2 5, 25)
countDecodings('1123'); // => 5 (1 1 2 3, 11 2 3, 1 12 3, 1 1 23, 11 23)
countDecodings('8'); // => 1
countDecodings('40'); // => 0 ('0' and '40' are not codes)
countDecodings('110'); // => 1 (1 10; '11' would leave a lone '0')
countDecodings('2105'); // => 1 (2 10 5)
countDecodings('05'); // => 0
countDecodings(''); // => 0
countDecodings('12a'); // throws TypeError