|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 1061. Lexicographically Smallest Equivalent String *- |
| 4 | +
|
| 5 | +
|
| 6 | +You are given two strings of the same length s1 and s2 and a string baseStr. |
| 7 | +
|
| 8 | +We say s1[i] and s2[i] are equivalent characters. |
| 9 | +
|
| 10 | +For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'. |
| 11 | +Equivalent characters follow the usual rules of any equivalence relation: |
| 12 | +
|
| 13 | +Reflexivity: 'a' == 'a'. |
| 14 | +Symmetry: 'a' == 'b' implies 'b' == 'a'. |
| 15 | +Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'. |
| 16 | +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. |
| 17 | +
|
| 18 | +Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2. |
| 19 | +
|
| 20 | + |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +
|
| 24 | +Input: s1 = "parker", s2 = "morris", baseStr = "parser" |
| 25 | +Output: "makkek" |
| 26 | +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]. |
| 27 | +The characters in each group are equivalent and sorted in lexicographical order. |
| 28 | +So the answer is "makkek". |
| 29 | +Example 2: |
| 30 | +
|
| 31 | +Input: s1 = "hello", s2 = "world", baseStr = "hold" |
| 32 | +Output: "hdld" |
| 33 | +Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r]. |
| 34 | +So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld". |
| 35 | +Example 3: |
| 36 | +
|
| 37 | +Input: s1 = "leet-code", s2 = "programs", baseStr = "source-code" |
| 38 | +Output: "aauaaaaada" |
| 39 | +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". |
| 40 | + |
| 41 | +
|
| 42 | +Constraints: |
| 43 | +
|
| 44 | +1 <= s1.length, s2.length, baseStr <= 1000 |
| 45 | +s1.length == s2.length |
| 46 | +s1, s2, and baseStr consist of lowercase English letters. |
| 47 | +
|
| 48 | +*/ |
| 49 | + |
| 50 | +class UnionFind { |
| 51 | + List<int> parent = List.filled(26, 0); |
| 52 | + |
| 53 | + UnionFind() { |
| 54 | + for (int i = 0; i < 26; ++i) { |
| 55 | + parent[i] = i; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + int find(int a) { |
| 60 | + if (a != parent[a]) { |
| 61 | + parent[a] = find(parent[a]); |
| 62 | + } |
| 63 | + return parent[a]; |
| 64 | + } |
| 65 | + |
| 66 | + void union(int a, int b) { |
| 67 | + int parentA = find(a); |
| 68 | + int parentB = find(b); |
| 69 | + |
| 70 | + if (parentA == parentB) return; |
| 71 | + |
| 72 | + if (parentA < parentB) { |
| 73 | + parent[parentB] = parentA; |
| 74 | + } else { |
| 75 | + parent[parentA] = parentB; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class A { |
| 81 | + String smallestEquivalentString(String s1, String s2, String baseStr) { |
| 82 | + UnionFind uf = UnionFind(); |
| 83 | + for (int i = 0; i < s1.length; ++i) { |
| 84 | + uf.union(s1.codeUnitAt(i) - 'a'.codeUnitAt(0), |
| 85 | + s2.codeUnitAt(i) - 'a'.codeUnitAt(0)); |
| 86 | + } |
| 87 | + |
| 88 | + StringBuffer sb = StringBuffer(); |
| 89 | + for (String ch in baseStr.split("")) { |
| 90 | + sb.writeCharCode( |
| 91 | + ('a'.codeUnitAt(0) + uf.find(ch.codeUnitAt(0) - 'a'.codeUnitAt(0)))); |
| 92 | + } |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | +} |
0 commit comments