2022-03-27 18:35:17 +08:00
< p > You are given a string < code > s< / code > , and an array of pairs of indices in the string < code > pairs< / code > where < code > pairs[i] = [a, b]< / code > indicates 2 indices(0-indexed) of the string.< / p >
< p > You can swap the characters at any pair of indices in the given < code > pairs< / code > < strong > any number of times< / strong > .< / p >
< p > Return the lexicographically smallest string that < code > s< / code > can be changed to after using the swaps.< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 18:35:17 +08:00
< pre >
< strong > Input:< / strong > s = " dcab" , pairs = [[0,3],[1,2]]
< strong > Output:< / strong > " bacd"
< strong > Explaination:< / strong >
Swap s[0] and s[3], s = " bcad"
Swap s[1] and s[2], s = " bacd"
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 18:35:17 +08:00
< pre >
< strong > Input:< / strong > s = " dcab" , pairs = [[0,3],[1,2],[0,2]]
< strong > Output:< / strong > " abcd"
< strong > Explaination: < / strong >
Swap s[0] and s[3], s = " bcad"
Swap s[0] and s[2], s = " acbd"
Swap s[1] and s[2], s = " abcd" < / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 18:35:17 +08:00
< pre >
< strong > Input:< / strong > s = " cba" , pairs = [[0,1],[1,2]]
< strong > Output:< / strong > " abc"
< strong > Explaination: < / strong >
Swap s[0] and s[1], s = " bca"
Swap s[1] and s[2], s = " bac"
Swap s[0] and s[1], s = " abc"
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = s.length < = 10^5< / code > < / li >
< li > < code > 0 < = pairs.length < = 10^5< / code > < / li >
< li > < code > 0 < = pairs[i][0], pairs[i][1] < s.length< / code > < / li >
< li > < code > s< / code > only contains lower case English letters.< / li >
< / ul >