Exchange elements in a string java

Give you a string s, and some "index pairs" array pairs in the string, where pairs[i] = [a, b] represents two indexes in the string (numbering starts from 0).

You can swap the characters at any pair of indexes in pairs as many times as you want.

Return the smallest string lexicographically that s can become after several exchanges.

Example 1:

Input: s = "dcab", pairs = [[0,3],[1,2]]
Output: "bacd"
Explanation:
exchange s[0] and s[3], s = "bcad"
exchange s[1 ] And s[2], s = "bacd"
Example 2:

Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
Output: "abcd"
Explanation:
Exchange s[0] and s[3], s = " "bcad"
exchanges s[0] and s[2], s = "acbd"
exchanges s[1] and s[2], s = "abcd"
Example 3:

Input: s = "cba", pairs = [[0,1],[1,2]]
Output: "abc"
Explanation:
exchange s[0] and s[1], s = "bca"
exchange s[1 ] And s[2], s = "bac"
exchange s[0] and s[1], s = "abc"

prompt:

1 <= s.length <= 10^5
0 <= pairs.length <= 10^5
0 <= pairs[i][0], pairs[i][1] <s.length
s contains only lowercase English letter

Source: LeetCode
Link: https://leetcode-cn.com/problems/smallest-string-with-swaps
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Idea: I’m a waste. I didn’t write this question. I originally wanted to perform string operations, but I negated the string when I thought about ten minutes, because even if it could be written, it was too complicated. Later I thought that this question is OK. Using and checking the collection is like putting on the template copied last time, but if you don't put it in, emmmm is still too good.
ps: If someone posted a comment in the comment area, I copied it directly

class Solution {
    
    
    public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
    
    
        //初始化连通图
        DisjointSetUnion dsu = new DisjointSetUnion(s.length());
        for(List<Integer> pair:pairs){
    
    
            //合并
            dsu.unionSet(pair.get(0),pair.get(1));
        }
        //使用map存储祖先节点到子节点列表的映射,存储并查集结果
        //例如s = "dcab", pairs = [[0,3],[1,2]]
        //0:[d,b]  1:[c,a]
        Map<Integer,List<Character>> map =new HashMap<Integer,List<Character>>();
        for(int i =0;i<s.length();i++){
    
    
            int parent = dsu.find(i);//找到该节点的父节点
            if(!map.containsKey(parent)){
    
    
                map.put(parent,new ArrayList<Character>());
            }
            map.get(parent).add(s.charAt(i));
        }
        //对map中的值进行排序
        for(Map.Entry<Integer,List<Character>> entry:map.entrySet()){
    
    
            Collections.sort(entry.getValue(),new Comparator<Character>(){
    
    
                public int compare(Character c1,Character c2){
    
    
                    return c2-c1;
                }
            });
        }
        StringBuffer sb = new StringBuffer();
        for(int i =0;i<s.length();i++){
    
    
            int x = dsu.find(i);
            List<Character> list =map.get(x);
            sb.append(list.remove(list.size()-1));
        }
        return sb.toString();

    }
}
//定义并差集类
class DisjointSetUnion{
    
    
    int n;  //并查集长度
    int[] rank;  //节点等级
    int[] f;   //存储对应的祖先节点

    //构造函数,初始化属性
    public DisjointSetUnion(int n){
    
    
        this.n = n;
        rank = new int[n];
        Arrays.fill(rank,1);
        f = new int[n];
        for(int i=0;i<n;i++){
    
    
            f[i] = i;
        }
    }

    //方法find,寻找给节点的祖先
    public int find(int x){
    
    
        return f[x] == x?x:(f[x]=find(f[x]));
    }

    //合并到一个图中,共同有一个祖先
    public void unionSet(int x, int y) {
    
    
        int fx=find(x),fy = find(y);
        if(fx==fy){
    
    
            return;
        }
        if(rank[fx]<rank[fy]){
    
    
            //swap(fx,fy);
            int temp = fx;
            fx=fy;
            fy=temp;
        }
        //fx级别高,要作为祖先
        rank[fx] +=rank[fy];
        f[fy] = fx;
    }

}

Guess you like

Origin blog.csdn.net/weixin_43824233/article/details/112471852