Lexicographically Smallest Largest¶
Table of Contents¶
- 1323. Maximum 69 Number (Easy)
- 3216. Lexicographically Smallest String After a Swap (Easy)
- 2697. Lexicographically Smallest Palindrome (Easy)
- 1881. Maximum Value after Insertion (Medium)
- 2734. Lexicographically Smallest String After Substring Operation (Medium)
- 1946. Largest Number After Mutating Substring (Medium)
- 1663. Smallest String With A Given Numeric Value (Medium)
- 1328. Break a Palindrome (Medium)
- 2259. Remove Digit From Number to Maximize Result (Easy)
- 2566. Maximum Difference by Remapping a Digit (Easy)
- 670. Maximum Swap (Medium)
- 3106. Lexicographically Smallest String After Operations With Constraint (Medium)
- 1053. Previous Permutation With One Swap (Medium)
- 2375. Construct Smallest Number From DI String (Medium)
- 2182. Construct String With Repeat Limit (Medium)
- 738. Monotone Increasing Digits (Medium)
- 3403. Find the Lexicographically Largest String From the Box I (Medium)
- 3170. Lexicographically Minimum String After Removing Stars (Medium)
- 1363. Largest Multiple of Three (Hard)
- 1754. Largest Merge Of Two Strings (Medium)
- 1202. Smallest String With Swaps (Medium)
- 2434. Using a Robot to Print the Lexicographically Smallest String (Medium)
- 1625. Lexicographically Smallest String After Applying Operations (Medium)
- 2948. Make Lexicographically Smallest Array by Swapping Elements (Medium)
- 564. Find the Closest Palindrome (Hard)
- 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits (Hard)
- 2663. Lexicographically Smallest Beautiful String (Hard)
- 3302. Find the Lexicographically Smallest Valid Sequence (Medium)
- 555. Split Concatenated Strings (Medium) 👑
- 3088. Make String Anti-palindrome (Hard) 👑
1323. Maximum 69 Number¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, greedy
3216. Lexicographically Smallest String After a Swap¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string, greedy
2697. Lexicographically Smallest Palindrome¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: two pointers, string, greedy
def makeSmallestPalindrome(s: str) -> str:
n = len(s)
s = list(s)
left, right = 0, n - 1
while left < right:
if s[left] < s[right]:
s[right] = s[left]
elif s[left] > s[right]:
s[left] = s[right]
left += 1
right -= 1
return "".join(s)
s = "egcfe"
print(makeSmallestPalindrome(s)) # "efcfe"
1881. Maximum Value after Insertion¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
2734. Lexicographically Smallest String After Substring Operation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
1946. Largest Number After Mutating Substring¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, string, greedy
1663. Smallest String With A Given Numeric Value¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
1328. Break a Palindrome¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
# Greedy
def breakPalindrome(palindrome: str) -> str:
n = len(palindrome)
if n == 1:
return ""
for i in range(n // 2):
if palindrome[i] != "a":
return palindrome[:i] + "a" + palindrome[i + 1 :]
return palindrome[:-1] + "b"
palindrome = "abccba"
print(breakPalindrome(palindrome)) # "aaccba"
2259. Remove Digit From Number to Maximize Result¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string, greedy, enumeration
2566. Maximum Difference by Remapping a Digit¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, greedy
670. Maximum Swap¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, greedy
3106. Lexicographically Smallest String After Operations With Constraint¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy
1053. Previous Permutation With One Swap¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
2375. Construct Smallest Number From DI String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, backtracking, stack, greedy
2182. Construct String With Repeat Limit¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, greedy, heap priority queue, counting
738. Monotone Increasing Digits¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, greedy
- Return the largest number that is less than or equal to
n
with monotone increasing digits.
# Greedy
def monotoneIncreasingDigits(n: int) -> int:
strNum = list(str(n))
for i in range(len(strNum) - 2, -1, -1):
if int(strNum[i]) > int(strNum[i + 1]):
strNum[i] = str(int(strNum[i]) - 1)
strNum[i + 1 :] = ["9"] * (len(strNum) - (i + 1))
return int("".join(strNum))
n = 332
print(monotoneIncreasingDigits(n)) # 299
3403. Find the Lexicographically Largest String From the Box I¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: two pointers, string, enumeration
3170. Lexicographically Minimum String After Removing Stars¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, stack, greedy, heap priority queue
1363. Largest Multiple of Three¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, greedy
1754. Largest Merge Of Two Strings¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: two pointers, string, greedy
1202. Smallest String With Swaps¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, string, depth first search, breadth first search, union find, sorting
from collections import defaultdict
from typing import List
# Union Find
def smallestStringWithSwaps(s: str, pairs: List[List[int]]) -> str:
n = len(s)
par = list(range(n))
components = defaultdict(list)
def find(node):
p = par[node]
while par[p] != p:
par[p] = par[par[p]]
p = par[p]
return p
def union(n1, n2):
p1, p2 = find(n1), find(n2)
if p1 != p2:
par[p1] = p2
for index, j in pairs:
union(index, j)
for index in range(n):
components[find(index)].append(index)
res = list(s)
for indices in components.values():
chars = sorted([s[index] for index in indices])
for index, char in zip(indices, chars):
res[index] = char
return "".join(res)
s = "dcab"
pairs = [[0, 3], [1, 2]]
print(smallestStringWithSwaps(s, pairs)) # "bacd"
2434. Using a Robot to Print the Lexicographically Smallest String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, stack, greedy
1625. Lexicographically Smallest String After Applying Operations¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, depth first search, breadth first search, enumeration
2948. Make Lexicographically Smallest Array by Swapping Elements¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, union find, sorting
564. Find the Closest Palindrome¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: math, string
1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, greedy, binary indexed tree, segment tree
2663. Lexicographically Smallest Beautiful String¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, greedy
3302. Find the Lexicographically Smallest Valid Sequence¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: two pointers, string, dynamic programming, greedy
555. Split Concatenated Strings¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, string, greedy
3088. Make String Anti-palindrome¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, greedy, sorting, counting sort