首页 > 其他分享 >[LeetCode] 1061. Lexicographically Smallest Equivalent String

[LeetCode] 1061. Lexicographically Smallest Equivalent String

时间:2023-01-26 03:33:05浏览次数:65  
标签:String parent 1061 s2 Equivalent 等价 int baseStr s1

You are given two strings of the same length s1 and s2 and a string baseStr.

We say s1[i] and s2[i] are equivalent characters.

  • For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c''b' == 'd', and '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 s1 = "abc" and s2 = "cde""acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.

Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2.

Example 1:

Input: s1 = "parker", s2 = "morris", baseStr = "parser"
Output: "makkek"
Explanation: Based on the equivalency information in s1 and s2, 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: s1 = "hello", s2 = "world", baseStr = "hold"
Output: "hdld"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".

Example 3:

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

Constraints:

  • 1 <= s1.length, s2.length, baseStr <= 1000
  • s1.length == s2.length
  • s1s2, and baseStr consist of lowercase English letters.

按字典序排列最小的等效字符串。

给出长度相同的两个字符串s1 和 s2 ,还有一个字符串 baseStr 。

其中  s1[i] 和 s2[i]  是一组等价字符。

举个例子,如果 s1 = "abc" 且 s2 = "cde",那么就有 'a' == 'c', 'b' == 'd', 'c' == 'e'。
等价字符遵循任何等价关系的一般规则:

 自反性 :'a' == 'a'
 对称性 :'a' == 'b' 则必定有 'b' == 'a'
 传递性 :'a' == 'b' 且 'b' == 'c' 就表明 'a' == 'c'
例如, s1 = "abc" 和 s2 = "cde" 的等价信息和之前的例子一样,那么 baseStr = "eed" , "acd" 或 "aab",这三个字符串都是等价的,而 "aab" 是 baseStr 的按字典序最小的等价字符串

利用 s1 和 s2 的等价信息,找出并返回 baseStr 的按字典序排列最小的等价字符串。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/lexicographically-smallest-equivalent-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是并查集。

这道题给的是两个字符串 s1 和 s2,两者等长,而且两者对应 index 上的字母是等价的。这里的等价意味着两个字母是可以互相替换的。题目同时又给了一个字符串 baseStr,请按照 s1 和 s2 的等价关系,返回 baseStr 的等价字符串,并需要做到字典序最小。

如果你并查集的题目做多了,看到类似 'a' == 'c', 'b' == 'd', 'c' == 'e' 这种等价关系,和传递性这种关键词,你就能想到用并查集来解决。并查集的特点是可以将同一个连通分量里的点聚在一起,并且声明其中一个点代表这个连通分量。所以这道题我们的思路是用并查集先把 a 里的字符和 b 里的字符连通起来,并用字典序最小的点做这个连通分量的父节点。之后我们遍历 baseStr 的时候,对于其中的每一个字母,我们去看一下这个字母是否能在并查集里的某个连通分量里找到,如果能找到,返回那个连通分量的父节点即可。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public String smallestEquivalentString(String s1, String s2, String baseStr) {
 3         UnionFind uf = new UnionFind(26);
 4         for (int i = 0; i < s1.length(); i++) {
 5             int a = s1.charAt(i) - 'a';
 6             int b = s2.charAt(i) - 'a';
 7             uf.union(a, b);
 8         }
 9         
10         StringBuilder sb = new StringBuilder();
11         for (int i = 0; i < baseStr.length(); i++) {
12             char c = baseStr.charAt(i);
13             sb.append((char) ('a' + uf.find(c - 'a')));
14         }
15         return sb.toString();
16     }
17 }
18 
19 class UnionFind {
20     int[] parent;
21     
22     public UnionFind(int n) {
23         parent = new int[n];
24         for (int i = 0; i < n; i++) {
25             parent[i] = i;
26         }
27     }
28     
29     public int find(int i) {
30         if (i == parent[i]) {
31             return i;
32         }
33         return parent[i] = find(parent[i]);
34     }
35     
36     public void union(int p, int q) {
37         int pRoot = find(p);
38         int qRoot = find(q);
39         // 谁的字典序小,谁就做父节点
40         if (pRoot < qRoot) {
41             parent[qRoot] = pRoot;
42         } else {
43             parent[pRoot] = qRoot;
44         }
45     }
46 }

 

LeetCode 题目总结

标签:String,parent,1061,s2,Equivalent,等价,int,baseStr,s1
From: https://www.cnblogs.com/cnoodle/p/17067533.html

相关文章

  • Codeforces Round #661 (Div. 3) D. Binary String To Subsequences(贪心/思维)
    https://codeforces.com/contest/1399/problem/D题目大意:长度为n的字符串s只由0和1组成。我们要把s分成最小数量的子序列,使得每一个子序列看起来像“010101...”或......
  • 学习笔记——NoSQL数据库;Redis概述;redis中常用的数据类型(key、string)
    2023-01-24一、NoSQL数据库1、NoSQL数据库的简介NoSQL(NoSQL=NotOnlySQL),即“不仅仅是SQL”,泛指非关系型的数据库。NosQL不依赖业务逻辑方式存储,而以简单的key-value模......
  • CString如何转COleDateTime
    可以用类COleDateTime  .ParseDateTime或者是用ColeVariant例子如下所示CString aa="1978-01-0108:08:08";  COleVariant v(aa);  v.ChangeType(VT_D......
  • abc214 F - Substrings
    题意:求给定字符串\(s\)的不同非空子序列个数要求被选入的位置两两不相邻\(n\le2e5\)思路:如果没有不相邻的要求怎么做?\(f_i\)表示考虑\(s[1..i]\),并且选\(i\)......
  • UVA11022 String Factoring
    简要题意给出一个字符串\(S\),你可以进行任意次(压缩)操作,每次操作可以把字符串中连续几个相同的部分压缩成相同的一个。操作可以嵌套进行。你需要求出操作后字符串的最小长......
  • std::string::resize() 对缓冲区一些用处
    如果需要一个缓冲区来暂存字符串会先定义一个char*的数组来实现存完后又给string赋值,感觉有点麻烦,寻思有什么方法可以更优雅点比如如下代码1voidCVTString::StrToWS......
  • string的一些知识
    sizeof(string)为32因为本质上string属于类,类中的成员是char,类的大小就是类中成员变量(非静态)加上指向虚函数表的指针以及指向虚基类表的指针加起来的和。这里string类只有......
  • 23/1/119-LeetCode 08:String to Integer (atoi)
    思路主要是对于前面的零,可以不用再去特殊判断了嘛。直接当成普通的数字直接算就好,反正算完之后ans=0,nodifference;对于超出范围,这个一直都是我不太注意的地方,这里max=2^......
  • C++获取含有中文字符的string长度
    :前言造车轮的时候要用到中文字符串的长度辨别,发现char的识别不准,进行了一番研究。>开始研究在Windows下,中文字符在C++中的内存占用为2字节,此时采用字符串长度获取函......
  • string 接收 char 随机数abcd
    packagecom.fqs.demo;importjava.util.Random;publicclassCharAB{//输出26个小写字母和26个大写字母publicstaticvoidmain(String[]args){......