subject :
Given a number , We translate it as a string according to the following rules :0 Translate into “a” ,1 Translate into “b”,……,11 Translate into “l”,……,25 Translate into “z”. A number may have more than one translation . Please program a function , Used to calculate how many different translation methods a number has .
Input : 12258
Output : 5
explain : 12258 Yes 5 Different translations , Namely "bccfi", "bwfi", "bczi", "mcfi" and "mzi"
First , It needs to be clear a-z, The corresponding number range is 0-25, That is, the maximum number combination cannot exceed 25( Less than or equal to 25).
Ideas : recursive .
Code :
class Solution:
def func(self , nums_str):
if len(nums_str) < 2:
return 1
elif len(nums_str) == 2:
if int(nums_str) > 25:
return 1
else:
return 2
return self.func(nums_str[1:]) + self.func(nums_str[2:])