I tried to do this question like this but some test cases are failing don't know why?