原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/

题目:

Given strings A and B of the same length, we say A[i] and B[i] are equivalent characters. For example, if A = "abc" and B = "cde", then we have 'a' == 'c', 'b' == 'd', 'c' == 'e'.

Equivalent characters follow the usual rules of any equivalence relation:

  • Reflexivity: 'a' == 'a'
  • Symmetry: 'a' == 'b' implies 'b' == 'a'
  • Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'

For example, given the equivalency information from A and B above, S = "eed""acd", and "aab" are equivalent strings, and "aab" is the lexicographically smallest equivalent string of S.

Return the lexicographically smallest equivalent string of S by using the equivalency information from A and B.

Example 1:

Input: A = "parker", B = "morris", S = "parser"
Output: "makkek"
Explanation: Based on the equivalency information in A and B, we can group their characters as [m,p], [a,o], [k,r,s], [e,i]. The characters in each group are equivalent and sorted in lexicographical order. So the answer is "makkek".

Example 2:

Input: A = "hello", B = "world", S = "hold"
Output: "hdld"
Explanation:  Based on the equivalency information in A and B, we can group their characters as [h,w], [d,e,o], [l,r]. So only the second letter 'o' in S is changed to 'd', the answer is "hdld".

Example 3:

Input: A = "leetcode", B = "programs", S = "sourcecode"
Output: "aauaaaaada"
Explanation:  We group the equivalent characters in A and B as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in S except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".

Note:

  1. String AB and S consist of only lowercase English letters from 'a''z'.
  2. The lengths of string AB and S are between 1 and 1000.
  3. String A and B are of the same length.

题解:

A and B are equal, for each index, the corresponding character in A and B should be in the same union.

When do the union, union by rank. a<c, a is c's parent.

Later, for each character of S, find its ancestor and append it to result.

Time Complexity: O((m+n)logm). m = A.length(), n = S.length(). find takes O(logm).

With path compression and union by rank, amatorize O(1).

Space: O(m).

AC Java:

 1 class Solution {
 2     Map<Character, Character> parent = new HashMap<>();
 3
 4     public String smallestEquivalentString(String A, String B, String S) {
 5         for(int i = 0; i<A.length(); i++){
 6             char a = A.charAt(i);
 7             char b = B.charAt(i);
 8
 9             if(find(a) != find(b)){
10                 union(a, b);
11             }
12         }
13
14         StringBuilder sb = new StringBuilder();
15         for(int i = 0; i<S.length(); i++){
16             char anc = find(S.charAt(i));
17             sb.append(anc);
18         }
19
20         return sb.toString();
21     }
22
23     private char find(char c){
24         parent.putIfAbsent(c, c);
25         if(c != parent.get(c)){
26             char anc = find(parent.get(c));
27             parent.put(c, anc);
28         }
29
30         return parent.get(c);
31     }
32
33     private void union(char a, char b){
34         char c1 = find(a);
35         char c2 = find(b);
36         if(c1 < c2){
37             parent.put(c2, c1);
38         }else{
39             parent.put(c1, c2);
40         }
41     }
42 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11297136.html

LeetCode 1061. Lexicographically Smallest Equivalent String相关推荐

  1. LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

    [Med] LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree ...

  2. leetcode 1662. Check If Two String Arrays are Equivalent(python)

    描述 Given two string arrays word1 and word2, return true if the two arrays represent the same string, ...

  3. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  4. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  5. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  6. LeetCode Reverse Words in a String III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

  7. LeetCode 1061. 按字典序排列最小的等效字符串(并查集)

    文章目录 1. 题目 2. 解题 1. 题目 给出长度相同的两个字符串:A 和 B,其中 A[i] 和 B[i] 是一组等价字符. 举个例子,如果 A = "abc" 且 B = ...

  8. 【LeetCode 1125】 Smallest Sufficient Team

    题目描述 In a project, you have a list of required skills req_skills, and a list of people. The i-th per ...

  9. LeetCode Reverse Vowels of a String(字符串中元音字符反转)

    题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...

最新文章

  1. 【Mac visual studio community使用技巧】打开vs如何查看解决方案等
  2. bzoj1196 [HNOI2006]公路修建问题
  3. 雪鹰领主服务器维护,《雪鹰领主》7月14日维护更新公告
  4. python中__init__和__new__方法的使用
  5. C 线程的使用~(下)
  6. java-JSON: Expected value at 1:0 错误
  7. leetcode 滑动窗口小结 (二)
  8. OJ1114: 逆序(数组)(C语言)
  9. lua 获取网络时间_Lua脚本引擎教程:学习路线
  10. python中怎样向字典中添加值_python的字典中,如何向指定路径添加值?
  11. 怎么样把百度搜索引入自己的网站JS实现(附源代码)
  12. C++读取XML树的建立和遍历
  13. C#模拟js的Json对象创建,操作
  14. 【渝粤教育】21秋期末考试宏微观经济学10545k1
  15. 创建数据库以及该数据库下的表单
  16. 关于交换机端口的Trunk和Access模式的分析与验证
  17. 软件测试培训两个月靠谱吗?
  18. JavaScript立即执行函数
  19. linux 使用 tmux 运行后台程序
  20. self的用法与意义(一)

热门文章

  1. python机器人编程——四轴UARM机械臂的运动控制(逆解)原理及python实现(上)
  2. 智慧工地解决方案的关键技术
  3. 红米4高配版_标注:2016060_官方线刷包_救砖包_解账户锁
  4. 指针类型和指向的数据类型
  5. 一曲相思用计算机弹,弹一曲离殇,奏一曲相思
  6. VUE指令大全(详解)
  7. 钉钉JSAPI鉴权,解决报错“签名校验无效”
  8. 历代权臣的唯一结局 孙权为什么要迫害陆逊?
  9. 常用的卷积神经网络-1-卷积和通道
  10. 钓鱼邮件翻倍:2021年Q4企业邮箱安全报告出炉