Hard? Editorial way
I start with X = 0. Each operation either increments (++X or X++) or decrements (--X or X--) the value of X by 1. So, I just iterate through the operations, adding 1 for "++" and subtracting 1 for "--".
I explore all reachable strings from s using BFS (or DFS) on the state space of strings, applying the two allowed operations until no new string appears. I keep a visited set to avoid cycles and always track the lexicographically smallest string seen. Since |s| ≤ 100 and every operation maps a string to another string of the same length with digits 0..9, the reachable state space is finite and BFS will terminate quickly in practice for the given constraints.
class Solution:
def findLexSmallestString(self, s: str, a: int, b: int) -> str:The key idea to solve this problem efficiently is to treat each element x in the array as an interval [x - k, x + k], representing all the possible values it can take after performing the allowed operation. To maximize the number of distinct elements, we can use a greedy approach: sort all these intervals by their right endpoint, then iterate through them while maintaining a pointer cur that tracks the smallest integer not yet used. For each interval [l, r], we first move cur to the maximum of its current value and the interval’s left endpoint (cur = max(cur, l)), ensuring we stay within the valid range. If cur is still less than or equal to r, we can “use” this value — we increment our answer count and move cur forward by one (cur += 1) to avoid reusing numbers.
I observe that every number can be transformed to any integer having the same residue modulo value (by adding/subtracting value repeatedly). So each element only contributes one token to its residue class r = x % value. To maximize the MEX we need to be able to form all integers 0,1,2,...,m-1. Forming integer i requires consuming one token from residue i % value. Thus I count how many tokens we have per residue, then greedily try to form integers i = 0,1,2,...: for each i I check whether counts[i % value] > 0; if yes I consume one and continue, otherwise the current i is the MEX.
class Solution:
def findSmallestInteger(self, nums: List[int], value: int) -> int:I first compute lenInc[i] = the length of the longest strictly-increasing subarray starting at index i (so lenInc[i] >= 1). A subarray of length k starting at i is strictly increasing iff lenInc[i] >= k. Given that, two adjacent strictly increasing subarrays of length k starting at a and a+k exist iff there is an index a with lenInc[a] >= k and lenInc[a+k] >= k.
class Solution:
def maxIncreasingSubarrays(self, nums: List[int]) -> int:I note a subarray of length k is strictly increasing iff each adjacent pair inside it is increasing, i.e. there are k-1 positions i..i+k-2 where nums[t] < nums[t+1]. So I build a binary array incpair where incpair[i] = 1 iff nums[i] < nums[i+1]. Then I build a prefix sum over incpair so I can test in O(1) whether any length-k window has k-1 increasing pairs. Finally I scan a from 0 to n - 2*k and check whether both windows starting at a and a+k are strictly increasing.
class Solution:
def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:I scan the list and keep a result stack. For each word, I compare its sorted characters to the sorted characters of the last word kept. If they are anagrams (sorted equal), I skip the current word (simulate deleting it). Otherwise I append it to the stack. At the end the stack is the final array after all deletions. This works because deletions only depend on the most recent kept word and the process is order-independent.
class Solution:
def removeAnagrams(self, words: List[str]) -> List[str]: