Topic:
A message containing letters from A-Z is 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 "n", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "is 2".
Translation:
A message has a A-Z of these letters, but the message is encoded as a number, in the following way:
‘A‘1‘B‘2...‘Z‘26
Give you a coded message that calculates the number of possible decoding methods.
For example, give you a message "12", which can be parsed as "AB" or "L" (12), so the number of this message decoding method is 2.
Analysis:
There are 2 ways to do this, one of which is a time-consuming 8 ms in Leetcode, and the way two is 2 Ms.
Way one: Assuming that the string is infinitely long, for example abxxxxxxx ..., we focus only on the first 2 digits.
1. When a is parsed independently, the possible parsing methods are Numdecodings (Bxxxxxxx ...). Two
2. When a and b combinations are resolved, the possible parsing methods are Numdecodings (XXXXXXX ...). Two
The final result is the and of the above 2 cases.
Way two:
1. First verify and convert the numbers in the string. We divide the numbers into 4 categories:
1) can also be removed or can not be removed, but not the beginning, for example 18 of 8, 26 of 6, with Y replacement
2) must be removed, and cannot begin, for example 28 of 8, directly replaced with X
3) can also be removed, but can begin, such as 11 of 1, do not modify
4) must be merged with the previous number, for example 10 of 0, replace the previous number and this number with XX
The x above indicates the partition, that is, the numbers before and after X are calculated separately, then multiply.
The above y represents a partition, similar to x, but Y participates in the previous number calculation.
2. After step one, the characters in the string are left only 1, 2, X, Y. where x, y as a partition, as long as the statistics are separated by the partition of the results of each group of numbers, and then multiply them together. And how to calculate the results of each group of numbers, by observing that the result of each group of numbers is related to the number of inputs, and conforms to the Fibonacci sequence.
Java version Code (mode 1):
Public classsolution091a {Private Staticmap<string, integer> resmap =NewHashmap<> (); Public int numdecodings(String s) {Char[] chars = S.tochararray ();intlen = chars.length;if(len = =0)return 0; Integer res1 =0; Integer Res2 =0;if(Len >=1) {CharCH = chars[0];if(ch = =' 0 ') {return 0; }Else{if(len = =1) {res1 =1; }Else{String substring = s.substring (1); Res1 = Resmap.Get(substring);if(Res1 = =NULL) {res1 = numdecodings (substring); Resmap.put (substring, res1); } } }if(Len >=2) {if(ch = =' 1 ') {if(len = =2) {Res2 =1; }Else{res2 = Numdecodings (s.substring (2)); } }Else if(ch = =' 2 ') {if(chars[1] >=' 0 '&& chars[1] <=' 6 ') {if(len = =2) {Res2 =1; }Else{String substring = s.substring (2); Res2 = Resmap.Get(substring);if(Res2 = =NULL) {res2 = numdecodings (substring); Resmap.put (substring, res2); } } } } } }returnRes1 + res2; }}
Java version Code (mode 2):
Public class solution091b {Private StaticMap<integer, integer> FMap =NewHashmap<> (); Public int numdecodings(String input) {if(Input = =NULL|| Input.length () = =0) {return 0; }Char[] chars = Input.tochararray ();if(!clean (chars))return 0;returnCal (chars); }/** * Verify and convert the numbers in the string * * @param chars * @return Verify the result is legal * @author L Nho * * Private Boolean Clean(Char[] chars) { for(inti =0; i < chars.length; i++) {Charcurrent = Chars[i];if(Current = =' 0 '|| (Current >=' 3 '&& Current <=' 9 ')) {if(i = =0) {if(Current = =' 0 ') {return false; }Else{//For the first digit, according to the situation (2)Chars[i] =' X ';Continue; } }CharPrevious = Chars[i-1];if(Previous! =' 1 '&& Previous! =' 2 ') {if(Current = =' 0 ') {return false; }Else{//Front partition (x, y), compliant (2)Chars[i] =' X ';Continue; } }if(Previous = =' 2 '&& Current >' 6 ') {//Compliance (2)Chars[i] =' X ';Continue; }if(Current = =' 0 ') {//Compliance (4)Chars[i-1] =' X '; Chars[i] =' X '; }Else{//Compliance (1)Chars[i] =' Y '; } }//Compliance (3)}return true; }/** * Calculation results * * @param chars After the previous step, only 1,2,x,y is included in the character array of the entry parameter * @return * @author /c6> Lnho * / Private int Cal(Char[] chars) {intCount =0;intsum =1; for(CharCurrent:chars) {if(Current = =' 1 '|| Current = =' 2 ') {count++; }Else if(Current = =' Y ') {sum *= Fibonacci (Count +1); Count =0; }Else{if(Count! =0) {sum *= Fibonacci (count); Count =0; } } }if(Count! =0) {sum *= Fibonacci (count); }returnSum }/** * Get Fibonacci values * * @param n * @return * @author Lnho * / Private int Fibonacci(intN) {if(n = =1|| n = =2) {returnN } Integer fn = Fmap.get (n);if(fn = =NULL) {fn = Fibonacci (N-1) + Fibonacci (N-2); Fmap.put (n, FN); }returnfn }}
Leet Code OJ 91. Decode Ways [Difficulty:medium]