Lexicographically Smallest String After Applying Operations
You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.
You can apply either of the following two operations any number of times and in any order on s:
- Add
ato all odd indices ofs(0-indexed). Digits past9are cycled back to0. - Rotate
sto the right bybpositions.
Return the lexicographically smallest string you can obtain by applying the above operations any number of times on s.
A string a is lexicographically smaller than a string b of the same length if, in the first position where a and b differ, string a has a character that appears earlier in the alphabet than the corresponding character in b.
s = "5525", a = 9, b = 2"2050"s = "74", a = 5, b = 1"24"Constraints
- 2 <= s.length <= 100
- s.length is even.
- s consists of digits from 0 to 9 only.
- 1 <= a <= 9
- 1 <= b <= s.length - 1
Asked at 2 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.