My explanation on why the number of states is small.
Let a state be (a, b, c, d) - the number of students in each house.
Suppose the sequence is all "?" - "?????????......?"
Then in a state (a, b, c, d), the largest and the smallest number can differ by at most 1. Let the smallest number be x, then x can take a value from 1 to n/4. And each of a, b, c, d can be either x or x+1. So totally there can be at most n/4 * 2^4 = 4*n states.
If the sequence contains some characters which are not "?", for example "??RR?G?H", I think intutively the number of states is smaller.