Lexicographically Smallest Equivalent String

Similar Problems:

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'`

‘b’ implies ‘b’

Symmetry: 'a' =`= 'a'`

‘b’ and ‘b’

Transitivity: 'a' =`= '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:

- String A, B and S consist of only lowercase English letters from ‘a’ – ‘z’.
- The lengths of string A, B and S are between 1 and 1000.
- String A and B are of the same length.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

// Blog link: https://code.dennyzhang.com/lexicographically-smallest-equivalent-string // Basic Ideas: Unionfind // // Complexity: Time O(n) Space O(n) type UF struct { parent []int } func constructor(size int) UF { parent := make([]int, 26) for i, _ := range parent { parent[i] = i } return UF{parent:parent} } func (uf *UF) union(x, y int) { p, q := uf.find(x), uf.find(y) if p<q { uf.parent[q] = p } else { uf.parent[p] = q } } func (uf *UF) find(x int) int { for uf.parent[x] != x { x = uf.parent[x] } return x } func smallestEquivalentString(A string, B string, S string) string { uf := constructor(26) for i:=0; i<len(A); i++ { p, q := int(rune(A[i])-'a'), int(rune(B[i])-'a') fmt.Println(p, q) uf.union(p, q) } res := make([]rune, len(S)) for i, ch := range S { res[i] = rune(uf.find(int(ch-'a'))+'a') } return string(res) }