Skip to content

Commit db45a25

Browse files
committed
feat: add solutions to lc problems: No.2716~2719
* No.2716.Minimize String Length * No.2717.Semi-Ordered Permutation * No.2718.Sum of Matrix After Queries * No.2719.Count of Integers
1 parent 215097e commit db45a25

38 files changed

+1912
-2
lines changed

solution/2500-2599/2559.Count Vowel Strings in Ranges/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ function vowelStrings(words: string[], queries: number[][]): number[] {
278278
const n = words.length;
279279
const s: number[] = new Array(n + 1).fill(0);
280280
for (let i = 0; i < n; ++i) {
281-
if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) {
281+
if (
282+
vowels.has(words[i][0]) &&
283+
vowels.has(words[i][words[i].length - 1])
284+
) {
282285
s[i + 1] = s[i] + 1;
283286
} else {
284287
s[i + 1] = s[i];

solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ function vowelStrings(words: string[], queries: number[][]): number[] {
250250
const n = words.length;
251251
const s: number[] = new Array(n + 1).fill(0);
252252
for (let i = 0; i < n; ++i) {
253-
if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) {
253+
if (
254+
vowels.has(words[i][0]) &&
255+
vowels.has(words[i][words[i].length - 1])
256+
) {
254257
s[i + 1] = s[i] + 1;
255258
} else {
256259
s[i + 1] = s[i];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [2716. 最小化字符串长度](https://leetcode.cn/problems/minimize-string-length)
2+
3+
[English Version](/solution/2700-2799/2716.Minimize%20String%20Length/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,重复执行下述操作 <strong>任意</strong> 次:</p>
10+
11+
<ul>
12+
<li>在字符串中选出一个下标 <code>i</code> ,并使 <code>c</code> 为字符串下标 <code>i</code> 处的字符。并在 <code>i</code> <strong>左侧</strong>(如果有)和 <strong>右侧</strong>(如果有)各 <strong>删除 </strong>一个距离 <code>i</code> <strong>最近</strong> 的字符 <code>c</code> 。</li>
13+
</ul>
14+
15+
<p>请你通过执行上述操作任意次,使 <code>s</code> 的长度 <strong>最小化</strong> 。</p>
16+
17+
<p>返回一个表示 <strong>最小化</strong> 字符串的长度的整数。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>s = "aaabc"
25+
<strong>输出:</strong>3
26+
<strong>解释:</strong>在这个示例中,s 等于 "aaabc" 。我们可以选择位于下标 1 处的字符 'a' 开始。接着删除下标 1 左侧最近的那个 'a'(位于下标 0)以及下标 1 右侧最近的那个 'a'(位于下标 2)。执行操作后,字符串变为 "abc" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 3 。</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>s = "cbbd"
32+
<strong>输出:</strong>3
33+
<strong>解释:</strong>我们可以选择位于下标 1 处的字符 'b' 开始。下标 1 左侧不存在字符 'b' ,但右侧存在一个字符 'b'(位于下标 2),所以会删除位于下标 2 的字符 'b' 。执行操作后,字符串变为 "cbd" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 3 。</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>s = "dddaaa"
39+
<strong>输出:</strong>2
40+
<strong>解释:</strong>我们可以选择位于下标 1 处的字符 'd' 开始。接着删除下标 1 左侧最近的那个 'd'(位于下标 0)以及下标 1 右侧最近的那个 'd'(位于下标 2)。执行操作后,字符串变为 "daaa" 。继续对新字符串执行操作,可以选择位于下标 2 的字符 'a' 。接着删除下标 2 左侧最近的那个 'a'(位于下标 1)以及下标 2 右侧最近的那个 'a'(位于下标 3)。执行操作后,字符串变为 "da" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 2 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
49+
<li><code>s</code> 仅由小写英文字母组成</li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
**方法一:哈希表**
57+
58+
题目实际上可以转化为求字符串中不同字符的个数,因此,我们只需要统计字符串中不同字符的个数即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串的长度;而 $C$ 是字符集的大小,本题中字符集为小写英文字母,因此 $C=26$。
61+
62+
<!-- tabs:start -->
63+
64+
### **Python3**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```python
69+
class Solution:
70+
def minimizedStringLength(self, s: str) -> int:
71+
return len(set(s))
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
public int minimizedStringLength(String s) {
81+
Set<Character> ss = new HashSet<>();
82+
for (int i = 0; i < s.length(); ++i) {
83+
ss.add(s.charAt(i));
84+
}
85+
return ss.size();
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int minimizedStringLength(string s) {
96+
unordered_set<char> ss(s.begin(), s.end());
97+
return ss.size();
98+
}
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func minimizedStringLength(s string) int {
106+
ss := map[rune]struct{}{}
107+
for _, c := range s {
108+
ss[c] = struct{}{}
109+
}
110+
return len(ss)
111+
}
112+
```
113+
114+
### **TypeScript**
115+
116+
```ts
117+
function minimizedStringLength(s: string): number {
118+
return new Set(s.split('')).size;
119+
}
120+
```
121+
122+
### **...**
123+
124+
```
125+
126+
```
127+
128+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [2716. Minimize String Length](https://leetcode.com/problems/minimize-string-length)
2+
3+
[中文文档](/solution/2700-2799/2716.Minimize%20String%20Length/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p>
8+
9+
<ul>
10+
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li>
11+
</ul>
12+
13+
<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p>
14+
15+
<p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> s = &quot;aaabc&quot;
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> In this example, s is &quot;aaabc&quot;. We can start by selecting the character &#39;a&#39; at index 1. We then remove the closest &#39;a&#39; to the left of index 1, which is at index 0, and the closest &#39;a&#39; to the right of index 1, which is at index 2. After this operation, the string becomes &quot;abc&quot;. Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;cbbd&quot;
29+
<strong>Output:</strong> 3
30+
<strong>Explanation:</strong> For this we can start with character &#39;b&#39; at index 1. There is no occurrence of &#39;b&#39; to the left of index 1, but there is one to the right at index 2, so we delete the &#39;b&#39; at index 2. The string becomes &quot;cbd&quot; and further operations will leave it unchanged. Hence, the minimized length is 3.&nbsp;
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> s = &quot;dddaaa&quot;
37+
<strong>Output:</strong> 2
38+
<strong>Explanation:</strong> For this, we can start with the character &#39;d&#39; at index 1. The closest occurrence of a &#39;d&#39; to its left is at index 0, and the closest occurrence of a &#39;d&#39; to its right is at index 2. We delete both index 0 and 2, so the string becomes &quot;daaa&quot;. In the new string, we can select the character &#39;a&#39; at index 2. The closest occurrence of an &#39;a&#39; to its left is at index 1, and the closest occurrence of an &#39;a&#39; to its right is at index 3. We delete both of them, and the string becomes &quot;da&quot;. We cannot minimize this further, so the minimized length is 2.
39+
</pre>
40+
41+
<div class="notranslate" style="all: initial;">&nbsp;</div>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
48+
<li><code>s</code> contains only lowercase English letters</li>
49+
</ul>
50+
51+
## Solutions
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
```python
58+
class Solution:
59+
def minimizedStringLength(self, s: str) -> int:
60+
return len(set(s))
61+
```
62+
63+
### **Java**
64+
65+
```java
66+
class Solution {
67+
public int minimizedStringLength(String s) {
68+
Set<Character> ss = new HashSet<>();
69+
for (int i = 0; i < s.length(); ++i) {
70+
ss.add(s.charAt(i));
71+
}
72+
return ss.size();
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
int minimizedStringLength(string s) {
83+
unordered_set<char> ss(s.begin(), s.end());
84+
return ss.size();
85+
}
86+
};
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
func minimizedStringLength(s string) int {
93+
ss := map[rune]struct{}{}
94+
for _, c := range s {
95+
ss[c] = struct{}{}
96+
}
97+
return len(ss)
98+
}
99+
```
100+
101+
### **TypeScript**
102+
103+
```ts
104+
function minimizedStringLength(s: string): number {
105+
return new Set(s.split('')).size;
106+
}
107+
```
108+
109+
### **...**
110+
111+
```
112+
113+
```
114+
115+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
int minimizedStringLength(string s) {
4+
unordered_set<char> ss(s.begin(), s.end());
5+
return ss.size();
6+
}
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func minimizedStringLength(s string) int {
2+
ss := map[rune]struct{}{}
3+
for _, c := range s {
4+
ss[c] = struct{}{}
5+
}
6+
return len(ss)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int minimizedStringLength(String s) {
3+
Set<Character> ss = new HashSet<>();
4+
for (int i = 0; i < s.length(); ++i) {
5+
ss.add(s.charAt(i));
6+
}
7+
return ss.size();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minimizedStringLength(self, s: str) -> int:
3+
return len(set(s))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function minimizedStringLength(s: string): number {
2+
return new Set(s.split('')).size;
3+
}

0 commit comments

Comments
 (0)