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?
- A. 13
- B. 18
- C. 15
- 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
- What is the maximum product of a non-empty contiguous subarray of [−2, 3, −4, 0, 5, −1, 2, −3, −1, 4]?
- The numbers 19, 5, 9, 18, 10, 29, 5, 24, 10 are split into two groups (every number goes into exactly one group). What is the smallest…
- A + or − sign is placed in front of each of the numbers 1, 1, 2, 3, 5, 8, 13, 21 (the two 1s are different items). In how many ways is the…
- An unbounded knapsack has capacity 47 and item types (weight, value): (5, 11), (8, 19), (9, 20), (13, 29), each available any number of…
- What is the largest sum of an increasing subsequence (strictly increasing, not necessarily contiguous) of [3, 100, 4, 5, 2, 200, 6, 7, 8…
- Two players alternately take a coin from either end of the row 20, 9, 12, 17, 1, 15, 8, 2 (the total is 84) and both play optimally to…