LeetCode 1061. Lexicographically Smallest Equivalent String
原题链接在这里: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 inA
andB
, 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 inA
andB
, we can group their characters as[h,w]
,[d,e,o]
,[l,r]
. So only the second letter'o'
inS
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 inA
andB
as[a,o,e,r,s,c]
,[l,p]
,[g,t]
and[d,m]
, thus all letters inS
except'u'
and'd'
are transformed to'a'
, the answer is"aauaaaaada"
.
Note:
- String
A
,B
andS
consist of only lowercase English letters from'a'
-'z'
. - The lengths of string
A
,B
andS
are between1
and1000
. - String
A
andB
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相关推荐
- 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 ...
- 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, ...
- 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 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
- LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
- LeetCode 1061. 按字典序排列最小的等效字符串(并查集)
文章目录 1. 题目 2. 解题 1. 题目 给出长度相同的两个字符串:A 和 B,其中 A[i] 和 B[i] 是一组等价字符. 举个例子,如果 A = "abc" 且 B = ...
- 【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 ...
- LeetCode Reverse Vowels of a String(字符串中元音字符反转)
题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...
最新文章
- 【Mac visual studio community使用技巧】打开vs如何查看解决方案等
- bzoj1196 [HNOI2006]公路修建问题
- 雪鹰领主服务器维护,《雪鹰领主》7月14日维护更新公告
- python中__init__和__new__方法的使用
- C 线程的使用~(下)
- java-JSON: Expected value at 1:0 错误
- leetcode 滑动窗口小结 (二)
- OJ1114: 逆序(数组)(C语言)
- lua 获取网络时间_Lua脚本引擎教程:学习路线
- python中怎样向字典中添加值_python的字典中,如何向指定路径添加值?
- 怎么样把百度搜索引入自己的网站JS实现(附源代码)
- C++读取XML树的建立和遍历
- C#模拟js的Json对象创建,操作
- 【渝粤教育】21秋期末考试宏微观经济学10545k1
- 创建数据库以及该数据库下的表单
- 关于交换机端口的Trunk和Access模式的分析与验证
- 软件测试培训两个月靠谱吗?
- JavaScript立即执行函数
- linux 使用 tmux 运行后台程序
- self的用法与意义(一)