Skip to content
Discussion options

You must be logged in to vote

We need to find the lexicographically smallest string by applying two operations:

  1. Add a to all odd indices (0-indexed), with digits wrapping around (mod 10)
  2. Rotate the string right by b positions

Since the string length is at most 100, I can use BFS to explore all possible states.

Approach

  1. Use BFS to explore all reachable string states
  2. Keep track of visited strings to avoid cycles
  3. For each string, generate two new strings:
    • One by applying the "add to odd indices" operation
    • One by applying the rotation operation
  4. Track the lexicographically smallest string found

Let's implement this solution in PHP: 1625. Lexicographically Smallest String After Applying Operations

<?php
/**
 * @param…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Oct 19, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 19, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants