zoukankan      html  css  js  c++  java
  • [LeetCode] 1202. Smallest String With Swaps

    You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.

    You can swap the characters at any pair of indices in the given pairs any number of times.

    Return the lexicographically smallest string that s can be changed to after using the swaps.

    Example 1:

    Input: s = "dcab", pairs = [[0,3],[1,2]]
    Output: "bacd"
    Explaination: 
    Swap s[0] and s[3], s = "bcad"
    Swap s[1] and s[2], s = "bacd"
    

    Example 2:

    Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
    Output: "abcd"
    Explaination: 
    Swap s[0] and s[3], s = "bcad"
    Swap s[0] and s[2], s = "acbd"
    Swap s[1] and s[2], s = "abcd"

    Example 3:

    Input: s = "cba", pairs = [[0,1],[1,2]]
    Output: "abc"
    Explaination: 
    Swap s[0] and s[1], s = "bca"
    Swap s[1] and s[2], s = "bac"
    Swap s[0] and s[1], s = "abc"

    Constraints:

    • 1 <= s.length <= 10^5
    • 0 <= pairs.length <= 10^5
    • 0 <= pairs[i][0], pairs[i][1] < s.length
    • s only contains lower case English letters.

    交换字符串中的元素。

    给你一个字符串 s,以及该字符串中的一些「索引对」数组 pairs,其中 pairs[i] = [a, b] 表示字符串中的两个索引(编号从 0 开始)。

    你可以 任意多次交换 在 pairs 中任意一对索引处的字符。

    返回在经过若干次交换后,s 可以变成的按字典序最小的字符串。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/smallest-string-with-swaps
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题意不难理解,题目给出的 pairs[i] 总能使某两个索引对相连,我们可以这样想,如果索引对多了,是不是有可能形成好几组索引对,每一组里面是多个索引相互关联的?比如题目给的第三个例子,0 - 1,1 - 2,那么我们可以得出 0,1,2 这三个索引是一组的。按照这样的思路,我们可以把解法往并查集上靠拢。

    对于 input 字符串里的每一个字母,首先我们需要找到其在并查集里面的祖先,记为 parent[i],这样对于每一个 index,我们都记录了它的父节点的 index。对于 input 给定的每一个 pairs 而言,我们通过 union find 确定一下这一对 index 到底谁是谁的父节点。此时我们再次遍历字符串的长度 len,对于遍历到的每个 index,因为之前通过 parent[i] 确认了每个 index 的父级 index 是谁,所以此时我们可以把父节点相同的字母通过 priority queue 放在一起。最后一轮再次遍历长度 len,这样我们可以通过 hashmap 找到每一组索引里面字典序最小的那个,直到把整个字符串都拼接好。

    时间O(V + E) - 其实是瞎写的~~

    空间O(V + E) - 其实是瞎写的~~

    Java实现

     1 class Solution {
     2     public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
     3         int len = s.length();
     4         int[] parent = new int[len];
     5         for (int i = 0; i < len; i++) {
     6             parent[i] = i;
     7         }
     8 
     9         for (List<Integer> pair : pairs) {
    10             int ancestry1 = find(pair.get(0), parent);
    11             int ancestry2 = find(pair.get(1), parent);
    12             parent[ancestry2] = ancestry1;
    13         }
    14 
    15         HashMap<Integer, PriorityQueue<Character>> map = new HashMap<>();
    16         for (int i = 0; i < len; i++) {
    17             int ancestry = find(i, parent);
    18             if (!map.containsKey(ancestry)) {
    19                 map.put(ancestry, new PriorityQueue<>());
    20             }
    21             map.get(ancestry).offer(s.charAt(i));
    22         }
    23 
    24         StringBuilder sb = new StringBuilder();
    25         for (int i = 0; i < len; i++) {
    26             PriorityQueue<Character> queue = map.get(find(i, parent));
    27             sb.append(queue.poll());
    28         }
    29         return sb.toString();
    30     }
    31 
    32     private int find(int i, int[] parent) {
    33         if (parent[i] != i) {
    34             parent[i] = find(parent[i], parent);
    35         }
    36         return parent[i];
    37     }
    38 }

    LeetCode 题目总结

  • 相关阅读:
    生成流畅文本方法
    KNNImputer:一种可靠的缺失值插补方法
    SparseNN中的优化
    基于TorchText的PyTorch文本分类
    蒙特卡洛方法分析Web页面浏览量
    D. Segment Intersections 模拟
    B. Array Walk
    线性基 P3857 [TJOI2008]彩灯
    线性基 P4570 [BJWC2011]元素
    线性基的学习+总结
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15233764.html
Copyright © 2011-2022 走看看