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 inAandB, 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 inAandB, we can group their characters as[h,w],[d,e,o],[l,r]. So only the second letter'o'inSis changed to'd', the answer is"hdld".
Example 3:
Input: A = "leetcode", B = "programs", S = "sourcecode"
Output: "aauaaaaada"
Explanation: We group the equivalent characters inAandBas[a,o,e,r,s,c],[l,p],[g,t]and[d,m], thus all letters inSexcept'u'and'd'are transformed to'a', the answer is"aauaaaaada".
Note:
- String
A,BandSconsist of only lowercase English letters from'a'-'z'. - The lengths of string
A,BandSare between1and1000. - String
AandBare 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:
class Solution {
Map<Character, Character> parent = new HashMap<>();
public String smallestEquivalentString(String A, String B, String S) {
for(int i = 0; i<A.length(); i++){
char a = A.charAt(i);
char b = B.charAt(i);
if(find(a) != find(b)){
union(a, b);
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i<S.length(); i++){
char anc = find(S.charAt(i));
sb.append(anc);
}
return sb.toString();
}
private char find(char c){
parent.putIfAbsent(c, c);
if(c != parent.get(c)){
char anc = find(parent.get(c));
parent.put(c, anc);
}
return parent.get(c);
}
private void union(char a, char b){
char c1 = find(a);
char c2 = find(b);
if(c1 < c2){
parent.put(c2, c1);
}else{
parent.put(c1, c2);
}
}
}
LeetCode 1061. Lexicographically Smallest Equivalent String的更多相关文章
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【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] ...
- 【leetcode】988. Smallest String Starting From Leaf
题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】761. Special Binary String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
随机推荐
- Django框架(十)--ORM多对多关联关系三种创建方式、form组件
多对多的三种创建方式 1.全自动(就是平常我们创建表多对多关系的方式) class Book(models.Model): title = models.CharField(max_length=32 ...
- Python全栈开发相关课程
Python全栈开发 Python入门 Python安装 Pycharm安装.激活.使用 Python基础 Python语法 Python数据类型 Python进阶 面向对象 网络编程 并发编程 数据 ...
- [cf 1236 E] Alice and the Unfair Game
题意: 给定一个长度为m的序列$A$,你有一个长度为n的棋盘,可以任选一个位置x作为起点. 在时刻$[1,m+1]$你可以向左或向右移动一格. 设时刻i你移动后所在的位置为$B_i$,你需要满足对于任 ...
- chrome Network 过滤和高级过滤
转自:https://blog.csdn.net/tengdazhang770960436/article/details/90644523
- Hyper-V虚拟机配置内部网络固定IP 并且连接外网
2019/10/23 Hyper-V CentOS7 摘要:Hyper-V中的虚拟机CentOS7能固定IP并且连接外网 保证宿主机的Xshell始终只用同一个IP连接到该虚拟机 新建内部网络虚拟交换 ...
- selenium中元素操作之浏览器窗口滚动&网页日期控件操作(js操作)(五)
js的滚动条scrollIntoView() Arguments[] - python与js之间的羁绊 1.移动到元素element对象的“底端”,与当前窗口的“底部”对齐: driver.execu ...
- window 包管理器--Chocolatey
Chocolatey 介绍 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocola ...
- 2019 竞技世界java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.竞技世界等公司offer,岗位是Java后端开发,最终选择去了竞技世界. 面试了很多家公司,感觉大部分公司考察的点 ...
- Matlab代码优化之道
一. 遵守Performance Acceleration的规则 关于什么是“Performance Acceleration”请参阅matlab的帮助文件.1.只有使用以下数据类型,matlab才会 ...
- 【转载】C#中使用float.TryParse方法将字符串转换为Float类型
在C#编程过程中,将字符串string转换为单精度float类型过程中,时常使用float.Parse方法,但float.Parse在无法转换的时候,会抛出程序异常,其实还有个float.TryPar ...