Advertisement

Lexicographically Smallest Equivalent String - LeetCode 1061 Solution

Lexicographically Smallest Equivalent String - Complete Solution Guide

Lexicographically Smallest Equivalent String is LeetCode problem 1061, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem requires finding the lexicographically smallest equivalent string of a given `baseStr` based on equivalency information provided by two strings `s1` and `s2` of the same length. Two characters `s1[i]` and `s2[i]` are considered equivalent. This equivalence relation follows reflexivity, symmetry, and transitivity. The goal is to replace each character in `baseStr` with the lexicographically smallest character in its equivalence class derived from the `s1` and `s2` pairs.

Solution Approach

The provided code uses the Union-Find algorithm to solve the problem. First, the algorithm iterates through `s1` and `s2`, establishing equivalency relationships between characters using the `union` operation. This operation merges the equivalence classes of two characters. Crucially, the `union` operation always links the character with the *larger* ASCII value to the character with the *smaller* ASCII value. This ensures that the root node of each equivalent set represents the smallest character in that set. Second, the algorithm iterates through `baseStr`. For each character, it finds the root of its equivalence class using the `find` operation (with path compression for efficiency). The `find` operation returns the representative (smallest) character of the equivalence class. Finally, the algorithm replaces the character in `baseStr` with the representative character.

Step-by-Step Algorithm

  1. Step 1: Initialize a `parent` array of size 26, where `parent[i] = i`. This represents that initially, each character ('a' + i) is only equivalent to itself.
  2. Step 2: Iterate through `s1` and `s2` simultaneously. For each index `i`, apply the `union` operation to the characters `s1[i]` and `s2[i]`. The `union` operation merges the equivalence classes of the characters represented by their indices.
  3. Step 3: The `union` operation first determines the roots of the equivalence classes that contain the charaters using the `find` operation. The `find` operation recursively follows the `parent` pointers until it reaches the root of the tree (the representative of the equivalence class). Path compression is implemented to optimize future lookups.
  4. Step 4: In the `union` operation, we compare the roots. The root with the larger ASCII value is made a child of the root with the smaller ASCII value. This ensures that the root will always represent the smallest character in the equivalent set.
  5. Step 5: Iterate through `baseStr`. For each character `c`, find the root of its equivalence class by using `find(c - 'a')`.
  6. Step 6: Append the character represented by the root node to the result string. `(char)(root + 'a')`.
  7. Step 7: Return the resulting string.

Key Insights

  • Insight 1: The core problem involves identifying equivalence classes of characters. The Union-Find data structure is perfectly suited to efficiently manage these equivalence relations.
  • Insight 2: The lexicographically smallest character within each equivalence class needs to be determined quickly for each character in `baseStr`. Union-Find with path compression ensures efficient finding of the 'root' or representative of each equivalence class, which will always be the smallest character.
  • Insight 3: Initializing the parent array with each element being its own parent is crucial. This represents that initially, each character is only equivalent to itself.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Union Find.

Companies

Asked at: Cloudera.