fork download
  1. def get_decoding_summary(digits: str) -> None:
  2. n = len(digits)
  3.  
  4. if n == 0 or digits[0] == '0':
  5. print(0)
  6. print([0])
  7. print("[]")
  8. return
  9.  
  10. dp = [0] * (n + 1)
  11. dp[0], dp[1] = 1, 1
  12.  
  13. for i in range(2, n + 1):
  14. if 1 <= int(digits[i-1:i]) <= 9:
  15. dp[i] += dp[i-1]
  16.  
  17. two_digits = int(digits[i-2:i])
  18. if 10 <= two_digits <= 26:
  19. dp[i] += dp[i-2]
  20.  
  21. if dp[i] == 0:
  22. print(0)
  23. print(dp[:i+1])
  24. print("[]")
  25. return
  26.  
  27. total_ways = dp[n]
  28.  
  29. mapping = {str(i): chr(64 + i) for i in range(1, 27)}
  30. results = []
  31.  
  32. def generate_paths(current_digits, current_path):
  33. if not current_digits:
  34. results.append(current_path)
  35. return
  36.  
  37. one = current_digits[0]
  38. if one in mapping:
  39. generate_paths(current_digits[1:], current_path + mapping[one])
  40.  
  41. if len(current_digits) >= 2:
  42. two = current_digits[:2]
  43. if two in mapping:
  44. generate_paths(current_digits[2:], current_path + mapping[two])
  45.  
  46. generate_paths(digits, "")
  47.  
  48. print(total_ways)
  49. print(dp)
  50. for path in results:
  51. print(path)
  52.  
  53. S = '1234567'
  54. get_decoding_summary(S)
Success #stdin #stdout 0.08s 14132KB
stdin
123
stdout
3
[1, 1, 2, 3, 3, 3, 3, 3]
ABCDEFG
AWDEFG
LCDEFG