Treat indices to as nodes.
Union indices and if there is a swap pair [i, j].
Group characters by their index's leader.
For each group, sort the characters.
Rebuild the string: for each position, take the next smallest available character from its group.
Time: for sorting. DSU part is nearly linear.
Space complexity is for the data structures used.