Shared Dance Moves
Two dance groups each have a routine written as one letter per move: S for a spin, J for a jump, P for a pose, and so on. For the joint finale they want to dance together, using moves that appear in both routines in the same order.
Each group may leave out moves but not reorder them. How long can the shared finale be?
Your task
Write sharedMoves(first, second). Return the length of the longest sequence of letters that can be made both from first and from second by deleting some letters. Kept letters stay in their original order but need not be next to each other.
If either routine is empty, or they share no move, return 0.
sharedMoves(first: string, second: string) → int
Examples
first = "SPJTS" second = "PSJST"
3
The moves P, J, T appear in order in both routines: at positions 1, 2 and 3 in SPJTS, and at positions 0, 2 and 4 in PSJST. S, J, S also works. No four moves appear in the same order in both, so the answer is 3.
first = "SJ" second = "JS"
1
Both have a spin and a jump, but in opposite orders. You can keep only one, so the answer is 1, not 2.
Constraints
- 0 ≤ first.length, second.length ≤ 1,000
- Both strings contain only uppercase letters A to Z.
Start with brute force
List every way to delete letters from the first routine (2ⁿ choices) and check whether each can also be made from the second. With 1,000 moves that is 2¹⁰⁰⁰ candidates.
Two tempting shortcuts are wrong. Counting common letters ignores order, so it says 2 for SJ and JS. Looking for a shared run of back-to-back moves misses moves that are spread out.
Hints
0 of 4 shown- Hint 1
Compare the last moves of the two routines. If they match, should the finale end with it? If they differ, at least one cannot be the finale's last move. Which?
- Hint 2
If the last letters match, the answer is 1 + the answer with both last letters removed. If they differ, drop the last letter of one routine or the other, and take the better result.
- Hint 3
So the answer depends only on two prefix lengths. Let
best[i][j]be the answer for the firstimoves offirstand the firstjofsecond. A row and column of zeros cover the empty prefixes. - Hint 4
Fill the table row by row: if
first[i - 1] === second[j - 1], thenbest[i][j] = best[i - 1][j - 1] + 1, otherwisemax(best[i - 1][j], best[i][j - 1]). The answer is in the bottom-right cell.
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
Look at the two last moves. If they are equal, the finale can end with that move, and the rest of it is the answer for both routines with that move removed. If they differ, at least one of them is not the last move of the finale, but you do not know which, so you try dropping each one and keep the larger result.
Trace the first example, SPJTS (rows) against PSJST (columns), with row 0 and column 0 as the empty prefixes:
| (empty) | P | S | J | S | T | |
|---|---|---|---|---|---|---|
| (empty) | 0 | 0 | 0 | 0 | 0 | 0 |
| S | 0 | 0 | 1 | 1 | 1 | 1 |
| P | 0 | 1 | 1 | 1 | 1 | 1 |
| J | 0 | 1 | 1 | 2 | 2 | 2 |
| T | 0 | 1 | 1 | 2 | 2 | 3 |
| S | 0 | 1 | 2 | 2 | 3 | 3 |
- In row S and the first column S, the letters match, so the cell is the diagonal value plus one: 0 + 1 = 1.
- In row P and column P, the letters match, so the cell is 0 + 1 = 1. The cell next to it compares P with S, which differ, so it takes the larger of the cell above (1) and the cell to the left (1), which is 1.
- In row J and column J, the letters match, so the cell is the diagonal value (row P, first column S) plus one: 1 + 1 = 2.
- In row T and column T, the letters match, so the cell is the diagonal value (row J, second column S) plus one: 2 + 1 = 3.
- In the last row S and column T, the letters differ, so the cell is the larger of the cell above (3) and the cell to the left (3), which is 3. The answer is 3.
For SJ against JS the rules give 1: the two matches sit on different diagonals, so neither builds on the other. Each cell reads only the row above and the cell to its left, so the code keeps just two rows.
function sharedMoves(first, second) {
const m = second.length;
// prev[j] = answer for the previous prefix of first and second[0..j)
let prev = new Array(m + 1).fill(0);
for (let i = 1; i <= first.length; i++) {
const cur = new Array(m + 1).fill(0);
for (let j = 1; j <= m; j++) {
if (first[i - 1] === second[j - 1]) cur[j] = prev[j - 1] + 1; // use the shared move
else cur[j] = Math.max(prev[j], cur[j - 1]); // drop one last move
}
prev = cur;
}
return prev[m];
}def shared_moves(first, second):
m = len(second)
# prev[j] = answer for the previous prefix of first and second[:j]
prev = [0] * (m + 1)
for a in first:
cur = [0] * (m + 1)
for j in range(1, m + 1):
if a == second[j - 1]:
cur[j] = prev[j - 1] + 1 # use the shared move
else:
cur[j] = max(prev[j], cur[j - 1]) # drop one last move
prev = cur
return prev[m]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.