the problem is arranging the parenthesis.
a open parenthesis should be paired to a nearest close parenthesis.
but if it has a unpaired parenthesis. parenthesis would be convert to a bracket. open parenthesis=open bracket close prnthsis. would be close brcket.
My suggestion:
1.) Copy the string
2.) replace each occurence of ("n times x") with x"n times x"x
3.) repeat step 2 until you can't replace anymore
4.) remaining ( or ) mark positions that have to be transformed to [ or ]