In short: The string is not stretchable, so every millimeter one mass moves, the string and the other mass move by the same distance at the same time. Both masses cover equal distances in equal times, so they share a common acceleration a.
1. Why must the two masses have the same acceleration? Because the string cannot stretch. Whatever distance one mass moves, the string and the other mass move the same distance at the same time.
2. What if one mass is much larger than the other? The larger mass still moves the same distance as the smaller one in the same time, so it has the same acceleration a.
3. Would this still hold if the string could stretch? No. The argument depends on the string being unstretchable. A stretchable string could let the masses move different distances.
Want more AP Physics 1 help like this?