博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 1061. Lexicographically Smallest Equivalent String
阅读量:4622 次
发布时间:2019-06-09

本文共 2358 字,大约阅读时间需要 7 分钟。

原题链接在这里:

题目:

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

Input: A = "hello", B = "world", S = "hold"Output: "hdld"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:

Input: A = "leetcode", B = "programs", S = "sourcecode"Output: "aauaaaaada"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
parent = new HashMap<>(); 3 4 public String smallestEquivalentString(String A, String B, String S) { 5 for(int i = 0; i

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11297136.html

你可能感兴趣的文章
团队编程项目作业2-团队编程项目设计文档
查看>>
html radio check
查看>>
Codeforces Round #312 (Div. 2)小结
查看>>
uboot 连接脚本分析
查看>>
usb host和usb device
查看>>
网站 wz
查看>>
20190221 beautiful soup 入门
查看>>
Contact Manager Web API 示例[2] Web API Routing
查看>>
GitHub----初学习(一)
查看>>
使用 Scut 搭建通服架构
查看>>
手机自动化测试:appium源码分析之bootstrap六
查看>>
微信小程序如何与数据库交互?
查看>>
XML DOM 循环(foreach)读取PHP数据 和 PHP 编写 XML DOM 【转载】
查看>>
带有public static void main方法的类,其中的成员变量必须是static的,否则main方法没法调用。除非是main里的局部变量。因为main方法就是static的啊。...
查看>>
Win10无法安装提示磁盘布局不受UEFI固件支持怎样解决
查看>>
爬虫大作业_爬取三星Galaxy_S9论坛
查看>>
自动化测试随笔4-无法点击底部的完成按钮
查看>>
Daily Scrum 10.29
查看>>
程序多开原理记录
查看>>
[转载]Oracle用户创建及权限设置
查看>>