原题链接在这里: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:

  1. Input: A = "parker", B = "morris", S = "parser"
  2. Output: "makkek"
  3. Explanation: Based on the equivalency information in A and B, 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:

  1. Input: A = "hello", B = "world", S = "hold"
  2. Output: "hdld"
  3. Explanation: Based on the equivalency information in A and B, we can group their characters as [h,w], [d,e,o], [l,r]. So only the second letter 'o' in S is changed to 'd', the answer is "hdld".

Example 3:

  1. Input: A = "leetcode", B = "programs", S = "sourcecode"
  2. Output: "aauaaaaada"
  3. Explanation: We group the equivalent characters in A and B as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in S except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".

Note:

  1. String AB and S consist of only lowercase English letters from 'a''z'.
  2. The lengths of string AB and S are between 1 and 1000.
  3. String A and B 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. }

LeetCode 1061. Lexicographically Smallest Equivalent String的更多相关文章

  1. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  2. 【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] ...

  3. 【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 ...

  4. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  5. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  6. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  7. 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 ...

  8. [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 ...

  9. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. Python之logging.basicConfig函数各参数

    filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所 ...

  2. CF241E Flights 差分约束

    传送门 差分约束永远是Itst最烂的图论知识点没有之一qwq 先用dfs把在\(1\)到\(N\)的路径上的所有点都拿出来,其他的点和边状态任意都不会影响答案. 然后考虑设\(dis_i\)表示从\( ...

  3. react-router的BrowserHistory 和 HashHistory 的区别,如何解决使用BrowserHistory 引起的访问路径问题

    一,使用createBrowserHistory 和 createHashHistory 的 区别体现 1. 使用createBrowserHistory () // 使用createBrowserH ...

  4. C# 录音和变调

    一直想研究下录音 正好有个项目有机会使用一下强大的 NAudio (https://github.com/naudio/NAudio)库 录音 NAudio 录音类库 public class NAu ...

  5. Visual Studio 2019 使用.Net Core 3.0 二

    一.遇到难题 在微软官方逛了一圈,看到了这个. 马上点击,进去看看什么情况. 1.安装previewVisual studio 2019 2.设置SDK previews in Visual Stud ...

  6. 防止用户重复提交表单数据,session方式,js方式

    1. 使用session的方式创建Token令牌解决 创建一个生成令牌的工具类,在该类中有返回类的对象,生成token的方法 public class TokenUtil { /* *单例设计模式(保 ...

  7. 自学Python编程的第五天(希望有IT大牛帮我看最下面的代码)----------来自苦逼的转行人

    2019-09-15-15:40:24 今天没有学知识,是一个一周总结,把这一周学的知识总结一遍,然后把做过的练习题再做一遍 看是否还会有再出现同样的错误,而且还可以知道有哪些知识点没有掌握好,可以把 ...

  8. Oracle建立连接的过程分析

    Oracle建立连接的过程 如果我们想登陆数据库并在数据库中真正做事情,就必须先建立连接,首先我会介绍如何建立连接,再介绍建立连接的两种方式的原理,以及建立连接的过程中在客户端和服务端都做了些什么. ...

  9. iOS学习——iOS项目增加新的字体

    基本思路 在项目开发过程中,iOS系统自带的字体库可能不适应需求,需要导入其他的字体库.下面是iOS项目增加新的字体的基本思路,基本上分为三步: 将字体库添加到项目中 在info.plist中添加所需 ...

  10. 源码解析-url状态检测神器ping-url

    前言 ping-url是我最近开源的一个小工具,这篇文章也是专门写它设计理念的科普文. 为什么会做这个ping-url开源工具呢? 起因是:本小哥在某天接到一个特殊的需求,要用前端的方式判断任意一个u ...