GATE Computer ScienceHardGATE CS (PYQ pattern)

A digit string is decoded using A = 1, B = 2, …, Z = 26, and each code must be a valid letter number (so 0 is allowed only as part of 10 or 20). In how many ways can 1226110211 be decoded?

  1. A. 13
  2. B. 18
  3. C. 15
  4. D. 21

Correct answer: C. 15

dp[i] = (dp[i − 1] if s[i] ≠ 0) + (dp[i − 2] if s[i − 1..i] is between 10 and 26). Running it gives 15; the single 0 (position 7) must be decoded together with the preceding 1 as 10, which removes some of the splits.

More Algorithms questions

Exams that ask this topic