Some test cases show wrong answer

This code passes 2 test cases but fails in the other two. Could you please check what the problem is?

@zenolusx2
Try this testcase
Input :
aabbbaaaa

Expected Output :
a2b3a4

Your Output :
a6b3

Thank you. I got it.