Title Link: https://leetcode.com/problems/decode-ways/
Topic:
A message containing letters from was A-Z
being encoded to numbers using the following mapping:
' A '-1 ' B '-2 ... ' Z '-26
Given an encoded message containing digits, determine the total number of ways to decode it.
for example,
Given encoded Message "
, it could be decoded as " AB "
(1 2) Or " L "
().
The number of ways decoding is "12"
2.
Ideas:
Similar to the stair climbing topic.
Algorithm:
public int numdecodings (String s) {if (s.length () = = 0) return 0; Char sc[] = S.tochararray (); if (sc[0] = = ' 0 ') return 0; int c[] = new Int[s.length ()]; C[0] = 1; for (int i = 1; i < sc.length; i++) {if ((sc[i-1] = = ' 2 ' && sc[i] >= ' 1 ' && sc[i] < = ' 6 ') | | (Sc[i-1] = = ' 1 ' && sc[i]! = ' 0 ')) {if (i + 1 < sc.length && Sc[i + 1] = = ' 0 ') {c[i] = c[i-1]; } else if (i-2 >= 0) {C[i] = C[i-1] + c[i-2];//similar stair climbing} else {C [i] = c[i-1] + 1; }} else if (sc[i] = = ' 0 ' && sc[i-1] = = ' 0 ') {return 0; } else if (sc[i] = = ' 0 ' && sc[i-1] > ' 2 ') {return 0; } else {C[i] = c[i-1]; }} return C[s.length ()-1]; }
"Leetcode" Decode Ways