Lexicographically Smallest String After Adjacent Removals Problem
#3563
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3563.
You are given a string s consisting of lowercase English letters.
You can perform the following operation any number of times (including zero):
- Remove any pair of adjacent characters in the string that are consecutive in the alphabet, in either order (e.g.,
'a'and'b', or'b'and'a'). - Shift the remaining characters to the left to fill the gap.
Return the lexicographically smallest string that can be obtained after performing the operations optimally.
Note: Consider the alphabet as circular, thus 'a' and 'z' are consecutive.
Example 1:
Input: s = "abc"
Output: "a"
Explanation:
- Remove "bc" from the string, leaving "a" as the remaining string.
- No further operations are possible. Thus, the lexicographically smallest string after all possible removals is "a".
Example 2:
Input: s = "bcda"
Output: ""
Explanation:
- Remove "cd" from the string, leaving "ba" as the remaining string.
- Remove "ba" from the string, leaving "" as the remaining string.
- No further operations are possible. Thus, the lexicographically smallest string after all possible removals is "".
Example 3:
Input: s = "zdce"
Output: "zdce"
Explanation:
- Remove "dc" from the string, leaving "ze" as the remaining string.
- No further operations are possible on "ze".
- However, since "zdce" is lexicographically smaller than "ze", the smallest string after all possible removals is "zdce".
Constraints:
1 <= s.length <= 250sconsists only of lowercase English letters.
Solution
Approach: Interval DP
Two tables:
can[i][j]says whether the substrings[i..j)can be removed completely. It is true for the empty range. Otherwises[i]must be paired with somes[k]that is consecutive with it in the alphabet (circularly), with everything between them removable (can[i+1][k]) and the rest removable (can[k+1][j]).best[i]is the smallest string that can remain from the suffixs[i..]. Eithers[i]stays, givings[i] + best[i+1], ors[i]is removed together with a blocks[i..j)thatcanremoves entirely, givingbest[j]. Keep the smaller one.
A character that stays splits the string, so pairs never cross it and the two parts are independent. The answer is best[0].
Complexity: time O(n^3) for can plus O(n^2) string comparisons of length up to n for best, space O(n^2).
class Solution:
def lexicographicallySmallestString(self, s: str) -> str:
n = len(s)
def near(a: str, b: str) -> bool:
d = abs(ord(a) - ord(b))
return d == 1 or d == 25
# can[i][j]: the substring s[i:j] can be removed completely
can = [[False] * (n + 1) for _ in range(n + 1)]
for i in range(n + 1):
can[i][i] = True
for length in range(2, n + 1, 2):
for i in range(n - length + 1):
j = i + length
for k in range(i + 1, j, 2):
if near(s[i], s[k]) and can[i + 1][k] and can[k + 1][j]:
can[i][j] = True
break
# best[i]: the smallest string that can remain from s[i:]
best = [""] * (n + 1)
for i in range(n - 1, -1, -1):
res = s[i] + best[i + 1]
for j in range(i + 2, n + 1, 2):
if can[i][j] and best[j] < res:
res = best[j]
best[i] = res
return best[0]