Secret Number Notes
Two friends pass notes in a simple code: each letter becomes its alphabet position (A is 1, Z is 26), and the numbers are written together with no spaces. "KZ" becomes 1126.
But 1126 could also be "AABF", "ALF" and more. Before guessing, the reader wants to know how many messages a note could mean.
Your task
Write countReadings(note). note is a string of digits. Split it into pieces, each a number from 1 to 26 written without a leading zero, and return how many different splits there are.
So "10" and "20" are valid pieces, but "0", "05" and "27" are not. If the note cannot be split at all, return 0.
countReadings(note: string) → int
Examples
note = "1126"
5
The splits are 1-1-2-6 (AABF), 11-2-6 (KBF), 1-12-6 (ALF), 1-1-26 (AAZ) and 11-26 (KZ). 126 is too big, so nothing else works.
note = "2105"
1
A 0 cannot stand alone, so it must end a 10 or 20. Here it pairs with the 1, forcing 2-10-5 (BJE). The split 21-0-5 fails on the lone 0.
Constraints
- 1 ≤ note.length ≤ 60
- note contains only the digits 0 to 9.
- The answer is below 10¹³.
Start with brute force
Recursively cut one or two digits off the front, check the piece, and count splits that reach the end. Correct, but a note of sixty 1s has about 2.5 trillion readings, and the recursion walks every one.
Most of that work is repeated. The number of readings of the rest of the note from position 30 is the same however the recursion got there, yet it recomputes that number for every path.
Hints
0 of 4 shown- Hint 1
The last piece of any reading of
1126is either6or26. What is left of the note in each case? - Hint 2
The rest is
112or11. Every reading of the full note is a reading of one of those prefixes plus the last piece. So you only need counts for shorter prefixes. - Hint 3
Let
ways[i]be the number of readings of the firstidigits, withways[0] = 1. Addways[i - 1]when digitialone is 1 to 9. Addways[i - 2]when the last two digits form 10 to 26. - Hint 4
Fill
waysleft to right. Watch the zeros: a single0adds nothing, and a pair starting with0(like05) is not 10 to 26, so it adds nothing either. Only the last two values are needed.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Every reading ends with a one-digit or a two-digit piece. So the number of readings of the first i digits is the number of readings of the first i - 1 digits (if the last digit is a valid piece) plus the number of readings of the first i - 2 digits (if the last two digits are a valid piece).
Trace "1126". The empty prefix has 1 reading.
- For "1", the digit 1 is valid, so there is 1 reading.
- For "11", the single 1 is valid (add 1) and the pair 11 is valid (add 1 from the empty prefix), so there are 2 readings.
- For "112", the single 2 is valid (add 2) and the pair 12 is valid (add 1), so there are 3 readings.
- For "1126", the single 6 is valid (add 3) and the pair 26 is valid (add 2), so there are 5 readings.
In "2105" the zero matters. At "210" the single 0 adds nothing, and the pair 10 adds the count for "2", which is 1. At "2105" the single 5 adds 1, and 05 is not a valid pair, so the answer is 1.
function countReadings(note) {
let prev2 = 1; // readings of the prefix two digits back (empty prefix: 1)
let prev1 = note[0] === "0" ? 0 : 1; // readings of the first digit
for (let i = 1; i < note.length; i++) {
let here = 0;
if (note[i] !== "0") here += prev1; // last piece is one digit
const pair = Number(note.slice(i - 1, i + 1));
if (pair >= 10 && pair <= 26) here += prev2; // last piece is two digits
prev2 = prev1;
prev1 = here;
}
return prev1;
}def count_readings(note):
prev2 = 1 # readings of the prefix two digits back (empty prefix: 1)
prev1 = 0 if note[0] == "0" else 1 # readings of the first digit
for i in range(1, len(note)):
here = 0
if note[i] != "0":
here += prev1 # last piece is one digit
pair = int(note[i - 1:i + 1])
if 10 <= pair <= 26:
here += prev2 # last piece is two digits
prev2, prev1 = prev1, here
return prev1Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.