Leetcode [91] (Java): Decode Ways (number of decoding methods)

Source: Internet
Author: User
Tags truncated

title : Number of decoding methods

Difficulty : Medium

topic content :

A message containing letters from was A-Z being encoded to numbers using the following mapping:

' A '-1 ' B '-2 ... ' Z '-26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

translation :

A message containing a-Z letter is encoded as a number, using the following mappings:

"A"-> 1

"B"-> 2

......

"Z"-> 26

Given a non-empty string containing a number, determine the total number of methods to decode it.

My idea : At first I wanted to use recursion but the boundary problem was too much to give up.

Single starts at 0 (each array is decoded separately), Count starts at 1, if s[i-1]s[i] the number of the two is within the (0,26] range, then count+;

And when you meet 0, the individual code at the beginning does not exist, and there is less chance of a combination, such as "110" in "11" and "0" can not be combined

So at this point, single = 0;count-1

My Code :

1      Public intnumdecodings (String s) {2         if(S.isempty () | | S.charat (0) = = ' 0 ')3             return0;4         5         intCount = 0;6         intSingle = 1;7          for(inti = 1; I < s.length (); i++) {8             if(S.charat (i) = = ' 0 '){9Count = S.length () > 2? Count-1: Count;TenSingle = 0; One             } A             intx = Integer.parseint (s.substring (i-1, i+1)); -             if(x<= && x > 0) { -Count + = 1; the             } -         } -         return(Count + single) > 0? (Count + single): 0; -}

results :214/258 Test cases passed.

Input:"1212" Output:4 expected:5

problems in the encoding process :

1. The original single and count are not calculated separately;

2, the 9th line, when only two, at this time count does not need-1, such as "10";

3, this idea still has the problem, for example "1212" in less calculated "12" "12" this combination

Answer code :

1  Public classSolution {2      Public intnumdecodings (String s) {3         intn =s.length ();4         if(n = = 0)return0;5         6         int[] Memo =New int[N+1];7Memo[n] = 1;8Memo[n-1] = S.charat (n-1)! = ' 0 '? 1:0;9         Ten          for(inti = n-2; I >= 0; i--) One             if(S.charat (i) = = ' 0 ')Continue; A             ElseMemo[i] = (Integer.parseint (s.substring (i,i+2)) <=26)? MEMO[I+1]+MEMO[I+2]: memo[i+1]; -          -         returnMemo[0]; the     } -}

Answer Ideas :

Assuming that all the numbers are valid, and that the left and right 22 adjacent numbers are also valid (1 to 26), then there are the following rules

numdecodings (s) = Numdecodings (s.substring (1)) + numdecodings (s.substring (2)) ---------------------------- ---------------------- -style (1)

The rule is obvious, Fibonacci sequence. It's just the opposite. (Forward from behind the string)

Of course, that's just a hypothesis, and a special case to consider:

" use an array (size of len+1) to record the corresponding number of digits after the" occurrence "of the number of decoded increments "

1, the current pointer refers to a character of 0

This character cannot be decoded at this time, so the former in the formula (1) can only be 0, the latter is also 0

For example "023", substring (1)--"0" | "23", the truncated "0" cannot be resolved, so this combination is invalid

SUBSTRING (2)--"02" | "3", the truncated "02" cannot be parsed, so this combination is invalid

So after 0 numbers appear, the number of decoding increments is 0.

2. The value of the current character is valid (greater than 0), but the number of the current character combined with the right character is not valid (greater than 26)

Equivalent (1) in the latter =0

For example "3212", substring (1)--"3" | "212", truncated "3" can parse, so its value is "212" decoding number

SUBSTRING (2)--"32" | "12", The truncated "32" cannot be parsed, so this combination is invalid \

Recursive implementations:

1      Public intnumdecodings (String s) {2         if(S.isempty ()) {3             return1;4         }5         6         if(S.charat (0) = = ' 0 ') {7             return0;8         } 9         Ten         if(s.length () = = 1) One             return1; A              -         intSub1 = Numdecodings (s.substring (1)); -         intSUB2 = 0; the          -         if(Integer.parseint (S.substring (0,2)) <= 26) { -Sub2 = Numdecodings (s.substring (2)); -         } +         returnSub1 +Sub2; -}

This method runs out in the last few use cases (very long).

So the answer takes the form of an iterative approach.

Optimization: Since it is a Fibonacci sequence, you can use a double-valued iteration instead of an array for recording .

1      Public intnumdecodings (String s) {2         if(S.isempty () | | S.charat (0) = = ' 0 ') {3             return0;4         }5         intSub1 = 1;6         intSUB2 = 1;7          for(inti = S.length ()-2; i >-1; i--) {8             if(S.charat (i+1) = = ' 0 ') {9Sub1 = 0;Ten             } One             if(Integer.parseint (S.substring (i,i+2)) <= 26) { ASub1 = Sub1 +Sub2; -SUB2 = Sub1-Sub2; -}Else { theSUB2 =sub1; -             } -         } -         returnsub1; +}

The 11th line can also be judged using characters to reduce the complexity of time and space:

S.charat (i) = = ' 1 ' | | S.charat (i) = = ' 2 ' && S.charat (i+1)-' 0 ' <= 6

Leetcode [91] (Java): Decode Ways (number of decoding methods)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.