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 个人 ...
随机推荐
- Delphi文字转语音TTS【支持选择语音库,播放,暂停,开始,停止,生成语音文件,设置音量,设置语速】
作者QQ:(648437169) 点击下载➨文字转语音TTS [Delphi 文字转语音TTS]调用系统自带的TTS组件,支持XP,vista,win7,win8,win10系统,支持选择语音库,播放 ...
- day01——python初始、变量、常量、注释、基础数据类型、输入、if
python的历史: 04年Django框架诞生了 内存回收机制是什么(面试题) python2:源码不统一,有重复的功能代码 python3:没有重复的功能代码 python是一个什么的编程语言 编 ...
- C语言学习笔记01——C语言概述
作者:Eventi 出处:http://www.cnblogs.com/Eventi 欢迎转载,也请保留这段声明.谢谢! 1 C语言的起源 1972年,贝尔实验室的丹尼斯·里奇(Dennis Ritc ...
- Visual Studio 2019激活
Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...
- CentOS7 搭建 NFS 服务器
环境: 系统版本:CentOS 7.5 一.服务端配置 1.配置环境 关闭防火墙服务 # 停止并禁用防火墙 $ systemctl stop firewalld $ systemctl disable ...
- 关于使用K8S的技术流程
部署Gogs版本管理系统 地址:https://gogs.io/docs 部署Harbor私有仓库 地址:https://github.com/goharbor/harbor/blob/master/ ...
- windows操作系统更改 <远程桌面> 端口号
windows远程桌面连接默认使用的是3389端口,为了避免被他人扫描从而暴力破解远程服务器或者病毒入侵.可以将默认端口修改为其它端口,如8888,11111等.最好修改为10000以后的端口,这样可 ...
- Mock、Powermock使用汇总
背景 工作中经常用到单测,某对单测掌握的不好,所以趁此学习.总结一下. 主要参考:https://www.jianshu.com/p/0c2480b1709e.https://www.cnblogs. ...
- webpack+vue-cil跨域配置接口地址代理
在vue项目开发的时候,接口联调的时候一般都是同域名下,且不存在跨域的情况下进行接口联调,但是当我们现在使用vue-cli进行项目打包的时候,我们在本地启动服务器后,比如本地开发服务下是 http:/ ...
- webdriver切换frame的方法
iframe: iframe 就是一个特殊的html 元素, 它在原来的html 范围内,开辟了一个新的HTML. iframe 元素会创建包含另外一个文档的内联框架(即行内框架) 理解:网页嵌套网页 ...