Total number of books is ten, and if all the books were on different subjects there would be 10!=3628800 permutations (arrangements) of books on the shelf. But 4 books are physics, and there are 4!=24 permutations of these but as far as the subject is concerned they are identical so, we need to reduce 10! by 24, that is: 10!/4!=151200. Likewise, there are 3!=6 perms of mathematics books, which reduces this number to 25200, and then to 12600 because there are 2 chemistry books.
If we take the case of a physics book at each end of the row of books, then we have 8 books between, 2 of which are physics books, and the other subjects the same as before. So we have 8!/((2!)(3!)(2!))=8!/24=1680. The probability of this is 1680/12600=2/15.
Now the mathematics books at each end, leaving one between: 8!/((4!)(1!)(2!))=840. The probability of this is 1/15.
Lastly, the chemistry books at each end: 8!/((4!)(3!))=280. The probability of this is 1/45.
There’s only one biology book so the probability of a biology book at each end is zero.
The probabilities are mutually exclusive so we simply add them together to find the probability of any of these circumstances occurring:
2/15+1/15+1/45+0=2/9 or about 22.2%.
So our answers appear to concur.