Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find lexicographically smallest string after applying operations in Python
Suppose we have a string s with only numeric digits and also have two values a and b. We can apply any one of the following two operations any number of times and in any order on s ?
Add 'a' to all odd positioned items of s (0-indexed). If digit is 9, then by adding something with it will be cycled back to 0.
Rotate 's' to the right by b positions.
We have to find the lexicographically smallest string we can get by applying the above operations any number of times on s.
Example Walkthrough
If the input is like s = "5323", a = 9, b = 2, then the output will be "2050" because if we follow ?
- Initial: "5323"
- Add operation: "5222" (add 9 to positions 1,3: 3+9=2, 3+9=2)
- Add operation: "5121" (add 9 to positions 1,3: 2+9=1, 2+9=1)
- Rotate right by 2: "2151"
- Add operation: "2050" (add 9 to positions 1,3: 1+9=0, 1+9=0)
Algorithm
To solve this, we will follow these steps ?
- Use BFS (Breadth-First Search) to explore all possible states
- Keep track of visited states to avoid cycles
- For each state, apply both operations and add new states to queue
- Return the lexicographically smallest string found
Implementation
from collections import deque
def add_operation(s, a):
result = ''
for idx, digit in enumerate(s):
if idx % 2 == 1: # odd position (0-indexed)
num = (int(digit) + a) % 10
result += str(num)
else:
result += digit
return result
def rotate_operation(s, b):
n = len(s)
b = b % n # handle cases where b > len(s)
return s[-b:] + s[:-b]
def find_smallest_string(s, a, b):
seen = set()
queue = deque([s])
while queue:
current = queue.popleft()
seen.add(current)
# Apply add operation
after_add = add_operation(current, a)
if after_add not in seen:
queue.append(after_add)
seen.add(after_add)
# Apply rotate operation
after_rotate = rotate_operation(current, b)
if after_rotate not in seen:
queue.append(after_rotate)
seen.add(after_rotate)
return min(seen)
# Test the solution
s = "5323"
a = 9
b = 2
result = find_smallest_string(s, a, b)
print(f"Input: s='{s}', a={a}, b={b}")
print(f"Lexicographically smallest string: {result}")
Input: s='5323', a=9, b=2 Lexicographically smallest string: 2050
How It Works
The algorithm uses BFS to explore all possible transformations of the input string. The add operation modifies digits at odd positions by adding 'a' and taking modulo 10. The rotate operation shifts the string right by 'b' positions. We continue until no new states can be generated, then return the lexicographically smallest string found.
Key Points
- BFS ensures we explore all reachable states systematically
- The set keeps track of visited states to prevent infinite loops
- Modulo 10 operation handles digit wraparound (9+1=0)
- Rotation is handled with string slicing for efficiency
Conclusion
This solution uses BFS to explore all possible string transformations and returns the lexicographically smallest result. The algorithm efficiently handles both add and rotate operations while avoiding cycles through state tracking.
