left
pointer to 0 and right
pointer to the length of the string minus one.left
is less than right
, do the following:
a. If the characters at left
and right
are the same, increment left
and decrement right
.
b. If they are not the same, find the index k
of the character from the right
side that matches the character at left
.
c. If no such k
is found (which means the character at left
is unique), swap it with the next character and increment the number of moves.
d. If k
is found, swap characters from k
to right - 1
to move the matching character to the right
position, incrementing the number of moves for each swap.
e. After the swaps, increment left
and decrement right
.