diff --git a/solution/0300-0399/0383.Ransom Note/README.md b/solution/0300-0399/0383.Ransom Note/README.md index 5c720a3b45638..44cabea0cfd21 100644 --- a/solution/0300-0399/0383.Ransom Note/README.md +++ b/solution/0300-0399/0383.Ransom Note/README.md @@ -136,7 +136,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/README_EN.md b/solution/0300-0399/0383.Ransom Note/README_EN.md index 2c792b73cd809..e3a3698e4ef77 100644 --- a/solution/0300-0399/0383.Ransom Note/README_EN.md +++ b/solution/0300-0399/0383.Ransom Note/README_EN.md @@ -113,7 +113,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/Solution.ts b/solution/0300-0399/0383.Ransom Note/Solution.ts index 66a65595b63e0..7fe6ae0705da8 100644 --- a/solution/0300-0399/0383.Ransom Note/Solution.ts +++ b/solution/0300-0399/0383.Ransom Note/Solution.ts @@ -1,5 +1,5 @@ function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md new file mode 100644 index 0000000000000..c626bbeccbab7 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md @@ -0,0 +1,160 @@ +# [10031. 大于等于顺序前缀和的最小缺失整数](https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) + +[English Version](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md) + +## 题目描述 + + + +
给你一个下标从 0 开始的整数数组 nums
。
如果一个前缀 nums[0..i]
满足对于 1 <= j <= i
的所有元素都有 nums[j] = nums[j - 1] + 1
,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含 nums[0]
的前缀也是一个 顺序前缀 。
请你返回 nums
中没有出现过的 最小 整数 x
,满足 x
大于等于 最长 顺序前缀的和。
+ +
示例 1:
+ ++输入:nums = [1,2,3,2,5] +输出:6 +解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。 ++ +
示例 2:
+ ++输入:nums = [3,4,5,1,12,14,13] +输出:15 +解释:nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。 ++ +
+ +
提示:
+ +1 <= nums.length <= 50
1 <= nums[i] <= 50
You are given a 0-indexed array of integers nums
.
A prefix nums[0..i]
is sequential if, for all 1 <= j <= i
, nums[j] = nums[j - 1] + 1
. In particular, the prefix consisting only of nums[0]
is sequential.
Return the smallest integer x
missing from nums
such that x
is greater than or equal to the sum of the longest sequential prefix.
+
Example 1:
+ ++Input: nums = [1,2,3,2,5] +Output: 6 +Explanation: The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ++ +
Example 2:
+ ++Input: nums = [3,4,5,1,12,14,13] +Output: 15 +Explanation: The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ++ +
+
Constraints:
+ +1 <= nums.length <= 50
1 <= nums[i] <= 50
给你一个下标从 0 开始的整数数组 nums
和一个正整数 k
。
你可以对数组执行以下操作 任意次 :
+ +0
变成 1
或者将 1
变成 0
。你的目标是让数组里 所有 元素的按位异或和得到 k
,请你返回达成这一目标的 最少 操作次数。
注意,你也可以将一个数的前导 0 翻转。比方说,数字 (101)2
翻转第四个数位,得到 (1101)2
。
+ +
示例 1:
+ ++输入:nums = [2,1,3,4], k = 1 +输出:2 +解释:我们可以执行以下操作: +- 选择下标为 2 的元素,也就是 3 == (011)2 ,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,1,2,4] 。 +- 选择下标为 0 的元素,也就是 2 == (010)2 ,我们翻转第三个数位得到 (110)2 == 6 。数组变为 [6,1,2,4] 。 +最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。 +无法用少于 2 次操作得到异或和等于 k 。 ++ +
示例 2:
+ ++输入:nums = [2,0,2,0], k = 0 +输出:0 +解释:数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。 ++ +
+ +
提示:
+ +1 <= nums.length <= 105
0 <= nums[i] <= 106
0 <= k <= 106
You are given a 0-indexed integer array nums
and a positive integer k
.
You can apply the following operation on the array any number of times:
+ +0
to 1
or vice versa.Return the minimum number of operations required to make the bitwise XOR
of all elements of the final array equal to k
.
Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2
you can flip the fourth bit and obtain (1101)2
.
+
Example 1:
+ ++Input: nums = [2,1,3,4], k = 1 +Output: 2 +Explanation: We can do the following operations: +- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4]. +- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4]. +The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. +It can be shown that we cannot make the XOR equal to k in less than 2 operations. ++ +
Example 2:
+ ++Input: nums = [2,0,2,0], k = 0 +Output: 0 +Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. ++ +
+
Constraints:
+ +1 <= nums.length <= 105
0 <= nums[i] <= 106
0 <= k <= 106
给你两个正整数 x
和 y
。
一次操作中,你可以执行以下四种操作之一:
+ +x
是 11
的倍数,将 x
除以 11
。x
是 5
的倍数,将 x
除以 5
。x
减 1
。x
加 1
。请你返回让 x
和 y
相等的 最少 操作次数。
+ +
示例 1:
+ ++输入:x = 26, y = 1 +输出:3 +解释:我们可以通过以下操作将 26 变为 1 : +1. 将 x 减 1 +2. 将 x 除以 5 +3. 将 x 除以 5 +将 26 变为 1 最少需要 3 次操作。 ++ +
示例 2:
+ ++输入:x = 54, y = 2 +输出:4 +解释:我们可以通过以下操作将 54 变为 2 : +1. 将 x 加 1 +2. 将 x 除以 11 +3. 将 x 除以 5 +4. 将 x 加 1 +将 54 变为 2 最少需要 4 次操作。 ++ +
示例 3:
+ ++输入:x = 25, y = 30 +输出:5 +解释:我们可以通过以下操作将 25 变为 30 : +1. 将 x 加 1 +2. 将 x 加 1 +3. 将 x 加 1 +4. 将 x 加 1 +5. 将 x 加 1 +将 25 变为 30 最少需要 5 次操作。 ++ +
+ +
提示:
+ +1 <= x, y <= 104
You are given two positive integers x
and y
.
In one operation, you can do one of the four following operations:
+ +x
by 11
if x
is a multiple of 11
.x
by 5
if x
is a multiple of 5
.x
by 1
.x
by 1
.Return the minimum number of operations required to make x
and y
equal.
+
Example 1:
+ ++Input: x = 26, y = 1 +Output: 3 +Explanation: We can make 26 equal to 1 by applying the following operations: +1. Decrement x by 1 +2. Divide x by 5 +3. Divide x by 5 +It can be shown that 3 is the minimum number of operations required to make 26 equal to 1. ++ +
Example 2:
+ ++Input: x = 54, y = 2 +Output: 4 +Explanation: We can make 54 equal to 2 by applying the following operations: +1. Increment x by 1 +2. Divide x by 11 +3. Divide x by 5 +4. Increment x by 1 +It can be shown that 4 is the minimum number of operations required to make 54 equal to 2. ++ +
Example 3:
+ ++Input: x = 25, y = 30 +Output: 5 +Explanation: We can make 25 equal to 30 by applying the following operations: +1. Increment x by 1 +2. Increment x by 1 +3. Increment x by 1 +4. Increment x by 1 +5. Increment x by 1 +It can be shown that 5 is the minimum number of operations required to make 25 equal to 30. ++ +
+
Constraints:
+ +1 <= x, y <= 104
给你三个整数 start
,finish
和 limit
。同时给你一个下标从 0 开始的字符串 s
,表示一个 正 整数。
如果一个 正 整数 x
末尾部分是 s
(换句话说,s
是 x
的 后缀),且 x
中的每个数位至多是 limit
,那么我们称 x
是 强大的 。
请你返回区间 [start..finish]
内强大整数的 总数目 。
如果一个字符串 x
是 y
中某个下标开始(包括 0
),到下标为 y.length - 1
结束的子字符串,那么我们称 x
是 y
的一个后缀。比方说,25
是 5125
的一个后缀,但不是 512
的后缀。
+ +
示例 1:
+ ++输入:start = 1, finish = 6000, limit = 4, s = "124" +输出:5 +解释:区间 [1..6000] 内的强大数字为 124 ,1124 ,2124 ,3124 和 4124 。这些整数的各个数位都 <= 4 且 "124" 是它们的后缀。注意 5124 不是强大整数,因为第一个数位 5 大于 4 。 +这个区间内总共只有这 5 个强大整数。 ++ +
示例 2:
+ ++输入:start = 15, finish = 215, limit = 6, s = "10" +输出:2 +解释:区间 [15..215] 内的强大整数为 110 和 210 。这些整数的各个数位都 <= 6 且 "10" 是它们的后缀。 +这个区间总共只有这 2 个强大整数。 ++ +
示例 3:
+ ++输入:start = 1000, finish = 2000, limit = 4, s = "3000" +输出:0 +解释:区间 [1000..2000] 内的整数都小于 3000 ,所以 "3000" 不可能是这个区间内任何整数的后缀。 ++ +
+ +
提示:
+ +1 <= start <= finish <= 1015
1 <= limit <= 9
1 <= s.length <= floor(log10(finish)) + 1
s
数位中每个数字都小于等于 limit
。s
不包含任何前导 0 。You are given three integers start
, finish
, and limit
. You are also given a 0-indexed string s
representing a positive integer.
A positive integer x
is called powerful if it ends with s
(in other words, s
is a suffix of x
) and each digit in x
is at most limit
.
Return the total number of powerful integers in the range [start..finish]
.
A string x
is a suffix of a string y
if and only if x
is a substring of y
that starts from some index (including 0
) in y
and extends to the index y.length - 1
. For example, 25
is a suffix of 5125
whereas 512
is not.
+
Example 1:
+ ++Input: start = 1, finish = 6000, limit = 4, s = "124" +Output: 5 +Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4. +It can be shown that there are only 5 powerful integers in this range. ++ +
Example 2:
+ ++Input: start = 15, finish = 215, limit = 6, s = "10" +Output: 2 +Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix. +It can be shown that there are only 2 powerful integers in this range. ++ +
Example 3:
+ ++Input: start = 1000, finish = 2000, limit = 4, s = "3000" +Output: 0 +Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range. ++ +
+
Constraints:
+ +1 <= start <= finish <= 1015
1 <= limit <= 9
1 <= s.length <= floor(log10(finish)) + 1
s
only consists of numeric digits which are at most limit
.s
does not have leading zeros.给你一个下标从 0 开始的二维整数数组 dimensions
。
对于所有下标 i
(0 <= i < dimensions.length
),dimensions[i][0]
表示矩形 i
的长度,而 dimensions[i][1]
表示矩形 i
的宽度。
返回对角线最 长 的矩形的 面积 。如果存在多个对角线长度相同的矩形,返回面积最 大 的矩形的面积。
+ ++ +
示例 1:
+ ++输入:dimensions = [[9,3],[8,6]] +输出:48 +解释: +下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487。 +下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。 +因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。 ++ +
示例 2:
+ ++输入:dimensions = [[3,4],[4,3]] +输出:12 +解释:两个矩形的对角线长度相同,为 5,所以最大面积 = 12。 ++ +
+ +
提示:
+ +1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
You are given a 2D 0-indexed integer array dimensions
.
For all indices i
, 0 <= i < dimensions.length
, dimensions[i][0]
represents the length and dimensions[i][1]
represents the width of the rectangle i
.
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.
+ ++
Example 1:
+ ++Input: dimensions = [[9,3],[8,6]] +Output: 48 +Explanation: +For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487. +For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10. +So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48. ++ +
Example 2:
+ ++Input: dimensions = [[3,4],[4,3]] +Output: 12 +Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12. ++ +
+
Constraints:
+ +1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
现有一个下标从 0 开始的 8 x 8
棋盘,上面有 3
枚棋子。
给你 6
个整数 a
、b
、c
、d
、e
和 f
,其中:
(a, b)
表示白色车的位置。(c, d)
表示白色象的位置。(e, f)
表示黑皇后的位置。假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。
+ +请注意:
+ ++ +
示例 1:
++输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 +输出:2 +解释:将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。 +由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。 ++ +
示例 2:
++输入:a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 +输出:1 +解释:可以通过以下任一方式移动 1 次捕获黑皇后: +- 将白色车移动到 (5, 2) 。 +- 将白色象移动到 (5, 2) 。 ++ +
+ +
提示:
+ +1 <= a, b, c, d, e, f <= 8
There is a 1-indexed 8 x 8
chessboard containing 3
pieces.
You are given 6
integers a
, b
, c
, d
, e
, and f
where:
(a, b)
denotes the position of the white rook.(c, d)
denotes the position of the white bishop.(e, f)
denotes the position of the black queen.Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.
+ +Note that:
+ ++
Example 1:
++Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 +Output: 2 +Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3). +It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning. ++ +
Example 2:
++Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 +Output: 1 +Explanation: We can capture the black queen in a single move by doing one of the following: +- Move the white rook to (5, 2). +- Move the white bishop to (5, 2). ++ +
+
Constraints:
+ +1 <= a, b, c, d, e, f <= 8
给你两个下标从 0
开始的整数数组 nums1
和 nums2
,它们的长度都是偶数 n
。
你必须从 nums1
中移除 n / 2
个元素,同时从 nums2
中也移除 n / 2
个元素。移除之后,你将 nums1
和 nums2
中剩下的元素插入到集合 s
中。
返回集合 s
可能的 最多 包含多少元素。
+ +
示例 1:
+ ++输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1] +输出:2 +解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。 +可以证明,在移除之后,集合 s 最多可以包含 2 个元素。 ++ +
示例 2:
+ ++输入:nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] +输出:5 +解释:从 nums1 中移除 2、3 和 6 ,同时从 nums2 中移除两个 3 和一个 2 。移除后,数组变为 nums1 = [1,4,5] 和 nums2 = [2,3,2] 。因此,s = {1,2,3,4,5} 。 +可以证明,在移除之后,集合 s 最多可以包含 5 个元素。 ++ +
示例 3:
+ ++输入:nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] +输出:6 +解释:从 nums1 中移除 1、2 和 3 ,同时从 nums2 中移除 4、5 和 6 。移除后,数组变为 nums1 = [1,2,3] 和 nums2 = [4,5,6] 。因此,s = {1,2,3,4,5,6} 。 +可以证明,在移除之后,集合 s 最多可以包含 6 个元素。+ +
+ +
提示:
+ +n == nums1.length == nums2.length
1 <= n <= 2 * 104
n
是偶数。1 <= nums1[i], nums2[i] <= 109
You are given two 0-indexed integer arrays nums1
and nums2
of even length n
.
You must remove n / 2
elements from nums1
and n / 2
elements from nums2
. After the removals, you insert the remaining elements of nums1
and nums2
into a set s
.
Return the maximum possible size of the set s
.
+
Example 1:
+ ++Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1] +Output: 2 +Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. +It can be shown that 2 is the maximum possible size of the set s after the removals. ++ +
Example 2:
+ ++Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] +Output: 5 +Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. +It can be shown that 5 is the maximum possible size of the set s after the removals. ++ +
Example 3:
+ ++Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] +Output: 6 +Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. +It can be shown that 6 is the maximum possible size of the set s after the removals. ++ +
+
Constraints:
+ +n == nums1.length == nums2.length
1 <= n <= 2 * 104
n
is even.1 <= nums1[i], nums2[i] <= 109
给你一个下标从 0 开始的字符串 s
和一个整数 k
。
你需要执行以下分割操作,直到字符串 s
变为 空:
s
的最长前缀,该前缀最多包含 k
个 不同 字符。s
中保持原来的顺序。执行操作之 前 ,你可以将 s
中 至多一处 下标的对应字符更改为另一个小写英文字母。
在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。
+ ++ +
示例 1:
+ ++输入:s = "accca", k = 2 +输出:3 +解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。 +s 变为 "acbca"。 +按照以下方式执行操作,直到 s 变为空: +- 选择最长且至多包含 2 个不同字符的前缀,"acbca"。 +- 删除该前缀,s 变为 "bca"。现在分割数量为 1。 +- 选择最长且至多包含 2 个不同字符的前缀,"bca"。 +- 删除该前缀,s 变为 "a"。现在分割数量为 2。 +- 选择最长且至多包含 2 个不同字符的前缀,"a"。 +- 删除该前缀,s 变为空。现在分割数量为 3。 +因此,答案是 3。 +可以证明,分割数量不可能超过 3。+ +
示例 2:
+ ++输入:s = "aabaab", k = 3 +输出:1 +解释:在此示例中,为了最大化得到的分割数量,可以保持 s 不变。 +按照以下方式执行操作,直到 s 变为空: +- 选择最长且至多包含 3 个不同字符的前缀,"aabaab"。 +- 删除该前缀,s 变为空。现在分割数量为 1。 +因此,答案是 1。 +可以证明,分割数量不可能超过 1。+ +
示例 3:
+ ++输入:s = "xxyz", k = 1 +输出:4 +解释:在此示例中,为了最大化得到的分割数量,可以将 s[1] 改为 'a'。 +s 变为 "xayz"。 +按照以下方式执行操作,直到 s 变为空: +- 选择最长且至多包含 1 个不同字符的前缀,"xayz"。 +- 删除该前缀,s 变为 "ayz"。现在分割数量为 1。 +- 选择最长且至多包含 1 个不同字符的前缀,"ayz"。 +- 删除该前缀,s 变为 "yz",现在分割数量为 2。 +- 选择最长且至多包含 1 个不同字符的前缀,"yz"。 +- 删除该前缀,s 变为 "z"。现在分割数量为 3。 +- 选择最且至多包含 1 个不同字符的前缀,"z"。 +- 删除该前缀,s 变为空。现在分割数量为 4。 +因此,答案是 4。 +可以证明,分割数量不可能超过 4。+ +
+ +
提示:
+ +1 <= s.length <= 104
s
只包含小写英文字母。1 <= k <= 26
You are given a 0-indexed string s
and an integer k
.
You are to perform the following partitioning operations until s
is empty:
s
containing at most k
distinct characters.s
and increase the number of partitions by one. The remaining characters (if any) in s
maintain their initial order.Before the operations, you are allowed to change at most one index in s
to another lowercase English letter.
Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
++
Example 1:
+ ++Input: s = "accca", k = 2 +Output: 3 +Explanation: In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'. +s becomes "acbca". +The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 2 distinct characters, "acbca". +- Delete the prefix, and s becomes "bca". The number of partitions is now 1. +- Choose the longest prefix containing at most 2 distinct characters, "bca". +- Delete the prefix, and s becomes "a". The number of partitions is now 2. +- Choose the longest prefix containing at most 2 distinct characters, "a". +- Delete the prefix, and s becomes empty. The number of partitions is now 3. +Hence, the answer is 3. +It can be shown that it is not possible to obtain more than 3 partitions.+ +
Example 2:
+ ++Input: s = "aabaab", k = 3 +Output: 1 +Explanation: In this example, to maximize the number of resulting partitions we can leave s as it is. +The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 3 distinct characters, "aabaab". +- Delete the prefix, and s becomes empty. The number of partitions becomes 1. +Hence, the answer is 1. +It can be shown that it is not possible to obtain more than 1 partition. ++ +
Example 3:
+ ++Input: s = "xxyz", k = 1 +Output: 4 +Explanation: In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'. +s becomes "xayz". +The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 1 distinct character, "xayz". +- Delete the prefix, and s becomes "ayz". The number of partitions is now 1. +- Choose the longest prefix containing at most 1 distinct character, "ayz". +- Delete the prefix, and s becomes "yz". The number of partitions is now 2. +- Choose the longest prefix containing at most 1 distinct character, "yz". +- Delete the prefix, and s becomes "z". The number of partitions is now 3. +- Choose the longest prefix containing at most 1 distinct character, "z". +- Delete the prefix, and s becomes empty. The number of partitions is now 4. +Hence, the answer is 4. +It can be shown that it is not possible to obtain more than 4 partitions. ++ +
+
Constraints:
+ +1 <= s.length <= 104
s
consists only of lowercase English letters.1 <= k <= 26