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 in
A
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 in
A
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 in
A
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:
- 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 个人 ...
随机推荐
- Python之logging.basicConfig函数各参数
filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所 ...
- CF241E Flights 差分约束
传送门 差分约束永远是Itst最烂的图论知识点没有之一qwq 先用dfs把在\(1\)到\(N\)的路径上的所有点都拿出来,其他的点和边状态任意都不会影响答案. 然后考虑设\(dis_i\)表示从\( ...
- react-router的BrowserHistory 和 HashHistory 的区别,如何解决使用BrowserHistory 引起的访问路径问题
一,使用createBrowserHistory 和 createHashHistory 的 区别体现 1. 使用createBrowserHistory () // 使用createBrowserH ...
- C# 录音和变调
一直想研究下录音 正好有个项目有机会使用一下强大的 NAudio (https://github.com/naudio/NAudio)库 录音 NAudio 录音类库 public class NAu ...
- Visual Studio 2019 使用.Net Core 3.0 二
一.遇到难题 在微软官方逛了一圈,看到了这个. 马上点击,进去看看什么情况. 1.安装previewVisual studio 2019 2.设置SDK previews in Visual Stud ...
- 防止用户重复提交表单数据,session方式,js方式
1. 使用session的方式创建Token令牌解决 创建一个生成令牌的工具类,在该类中有返回类的对象,生成token的方法 public class TokenUtil { /* *单例设计模式(保 ...
- 自学Python编程的第五天(希望有IT大牛帮我看最下面的代码)----------来自苦逼的转行人
2019-09-15-15:40:24 今天没有学知识,是一个一周总结,把这一周学的知识总结一遍,然后把做过的练习题再做一遍 看是否还会有再出现同样的错误,而且还可以知道有哪些知识点没有掌握好,可以把 ...
- Oracle建立连接的过程分析
Oracle建立连接的过程 如果我们想登陆数据库并在数据库中真正做事情,就必须先建立连接,首先我会介绍如何建立连接,再介绍建立连接的两种方式的原理,以及建立连接的过程中在客户端和服务端都做了些什么. ...
- iOS学习——iOS项目增加新的字体
基本思路 在项目开发过程中,iOS系统自带的字体库可能不适应需求,需要导入其他的字体库.下面是iOS项目增加新的字体的基本思路,基本上分为三步: 将字体库添加到项目中 在info.plist中添加所需 ...
- 源码解析-url状态检测神器ping-url
前言 ping-url是我最近开源的一个小工具,这篇文章也是专门写它设计理念的科普文. 为什么会做这个ping-url开源工具呢? 起因是:本小哥在某天接到一个特殊的需求,要用前端的方式判断任意一个u ...