给你一个字符串 s,以及该字符串中的一些「索引对」数组 pairs,其中 pairs[i] = [a, b] 表示字符串中的两个索引(编号从 0 开始)。
你可以 任意多次交换 在 pairs 中任意一对索引处的字符。
返回在经过若干次交换后,s 可以变成的按字典序最小的字符串。
示例 1:
输入:s = “dcab”, pairs = [[0,3],[1,2]]
输出:“bacd”
解释:
交换 s[0] 和 s[3], s = “bcad”
交换 s[1] 和 s[2], s = “bacd”
示例 2:
输入:s = “dcab”, pairs = [[0,3],[1,2],[0,2]]
输出:“abcd”
解释:
交换 s[0] 和 s[3], s = “bcad”
交换 s[0] 和 s[2], s = “acbd”
交换 s[1] 和 s[2], s = “abcd”
示例 3:
输入:s = “cba”, pairs = [[0,1],[1,2]]
输出:“abc”
解释:
交换 s[0] 和 s[1], s = “bca”
交换 s[1] 和 s[2], s = “bac”
交换 s[0] 和 s[1], s = “abc”
提示:
1 <= s.length <= 10^5
0 <= pairs.length <= 10^5
0 <= pairs[i][0], pairs[i][1] < s.length
s 中只含有小写英文字母
class UnionFind{
private:vector<int> parent;
public:UnionFind(int n){parent.resize(n);for(int i = 0; i < n; i++){parent[i] = i;}}void unite(int index1, int index2){parent[find(index2)] = find(index1);}int find(int index){if(parent[index] == index){return index;}parent[index] = find(parent[index]);return parent[index];}
};class Solution {
public:string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {int n = s.size();UnionFind uf(n);for(auto& c : pairs){uf.unite(c[0], c[1]);}unordered_map<int, priority_queue<char, vector<char>, greater<>>> hash;for(int i = 0; i < n; i++){auto& c = s[i];hash[uf.find(i)].push(c);}string res = "";for(int i = 0; i < n; i++){res += hash[uf.find(i)].top();hash[uf.find(i)].pop();}return res;}
};
这道题首先需要掌握并查集的知识,不熟悉该数据结构的可以先去看我主页力扣990题解。这道题目我们通过观察可以发现,只要索引对之间有交互,那么这些索引对所覆盖的索引的字符之间可以通过多次交换而替换成其中的任意一个字符。通过这个特性,我们就可以将索引对之间有交集的字符作为一个连通块,连通块为了排序字符之间的字典序,所以采用了小根堆的数据结构,保证连通块之间的字符是按字典序升序排列。
那么我们最后就可以定义一个变量res来记录答案,我们只需要遍历字符串从0到n-1的索引,然后将该字符索引所在连通集中的字典序最小的字符推入到res中,然后再将最小字符从连通集的小根堆中弹出即可。
举个例子
输入:s = “dcab”, pairs = [[0,3],[1,2]]
字符d和b是一个连通集,字符c和a是一个连通集。
那么在计算res的时候,遍历字符串s,i=0所在连通集b是字典序最小字符,那么就将b推入res中,然后i=1所在连通集a是字典序最小的字符,将a推入到res中,再遍历到i=2,此时该连通集的小根堆中只剩下了c,那么只能推入c到res中,最后i=3,此时该连通集中的小根堆只剩下了d,推入d到res中,最后输出:“bacd”。