bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1061. Lexicographically Smallest Equivalent String

1061. Lexicographically Smallest Equivalent String

Leetcode Solutions

Approach: Depth-First Search (DFS)

  1. Initialize an adjacency matrix adjMatrix of size 26x26 to represent the graph.
  2. Iterate over s1 and s2 and add edges between equivalent characters in the adjacency matrix.
  3. Initialize a visited array to keep track of visited nodes during DFS.
  4. For each character, if it's not visited, perform DFS to find all characters in its connected component and the smallest character within it.
  5. Create a mapping from each character to the smallest character in its connected component.
  6. Iterate over baseStr and build the answer string by replacing each character with its mapped smallest character.
  7. Return the answer string.
UML Thumbnail

Approach: Disjoint Set Union (DSU/Union Find)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...