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 个人 ...
随机推荐
- day13——重要内置函数、匿名函数、闭包
day13 内置函数2 重要的 abs():求绝对值--返回的都是正数 # lst = [-1,-2,-3] # for i in lst: # print(abs(i)) # print([abs( ...
- flink checkpoint状态储存三种方式选择
Flink 提供了三种可用的状态后端:MemoryStateBackend,FsStateBackend,和RocksDBStateBackend. MemoryStateBackend Memory ...
- ubuntu 使用阿里云镜像源快速搭建kubernetes 1.15.2集群
一.概述 搭建k8s集群时,需要访问google,下载相关镜像以及安装软件,非常麻烦. 正好阿里云提供了k8s的更新源,国内用户就可以直接使用了. 二.环境介绍 操作系统 主机名 IP地址 功能 配置 ...
- 【题解】Luogu P5360 [SDOI2019]世界地图
原题传送门 每次查询的实际就是将地图的一个前缀和一个后缀合并后的图的最小生成树边权和 我们要预处理每个前缀和后缀的最小生成树 实际求前缀和(后缀和)的过程珂以理解为上一个前缀和这一列的最小生成树进行合 ...
- 20、Outer Apply 和 Cross Apply
1.場合 select...caseが複雑の時 2.運用方法 SELECT * FROM stu CROSS APPLY ( --like inner join * FROM score WHERE ...
- integer 面试题。
上面输出的结果是:true true ----------------------------------------------------- false true 因为-128-127是byte的 ...
- 使用vue-cli搭建vue项目问题解决方案
工欲善其事必先利其器,安装所需环境 node和npm的安装 首先需要安装node环境,直接到官网下载安装包 https://nodejs.org/zh-cn/ 安装node默认安装npm, 不需要重复 ...
- springCloud学习1(集中式配置管理)
springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 一.前言 在开发普通的 web 应用中,我们通常是将配置项写在单 ...
- vue从零开始(三)指令
v-bind的使用 <!-- 绑定一个属性 --> <img v-bind:src="imageSrc"> <!-- 动态特性名 (2.6.0+) - ...
- JavaScript变量存储浅析(一)
Hello! 上一篇关于JS中函数传参(http://www.cnblogs.com/souvenir/p/4969092.html)的介绍中提到了JS的另外一个基本概念:JS变量存储, 今天我们就用 ...