Leetcode Problem 1625. Lexicographically Smallest String After Applying Operations

1625. Lexicographically Smallest String After Applying Operations

Leetcode Solutions

BFS Approach to Find Lexicographically Smallest String

  1. Initialize a queue and add the original string s to it.
  2. Initialize a set to keep track of seen strings and add s to it.
  3. Initialize a variable to store the smallest string seen so far and set it to s.
  4. While the queue is not empty: a. Dequeue the front string from the queue. b. Generate a new string by adding a to all odd indices of the dequeued string and check if it has been seen. If not, add it to the queue and set. c. Generate a new string by rotating the dequeued string by b positions and check if it has been seen. If not, add it to the queue and set. d. Update the smallest string if any of the new strings are lexicographically smaller.
  5. Return the smallest string found.
UML Thumbnail

DFS Approach to Find Lexicographically Smallest String

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...