The solution is correct, at least for the input in consideration:
In the pic above I clicked on Test and it resulted in runtime error. But if I run the same code with the same input as custom case:
!-----> I am a new user so only one image per comment. See next comment for continuation