Skip to content

Commit ff62ef9

Browse files
authored
feat: add solutions to lc problems: No.2712,2716 (#4260)
* No.2712.Minimum Cost to Make All Characters Equal * No.2716.Minimize String Length
1 parent f0ca39c commit ff62ef9

File tree

7 files changed

+81
-10
lines changed

7 files changed

+81
-10
lines changed

solution/2700-2799/2712.Minimum Cost to Make All Characters Equal/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ function minimumCost(s: string): number {
156156
}
157157
```
158158

159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn minimum_cost(s: String) -> i64 {
164+
let mut ans = 0;
165+
let n = s.len();
166+
let s = s.as_bytes();
167+
for i in 1..n {
168+
if s[i] != s[i - 1] {
169+
ans += i.min(n - i);
170+
}
171+
}
172+
ans as i64
173+
}
174+
}
175+
```
176+
159177
<!-- tabs:end -->
160178

161179
<!-- solution:end -->

solution/2700-2799/2712.Minimum Cost to Make All Characters Equal/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ function minimumCost(s: string): number {
155155
}
156156
```
157157

158+
#### Rust
159+
160+
```rust
161+
impl Solution {
162+
pub fn minimum_cost(s: String) -> i64 {
163+
let mut ans = 0;
164+
let n = s.len();
165+
let s = s.as_bytes();
166+
for i in 1..n {
167+
if s[i] != s[i - 1] {
168+
ans += i.min(n - i);
169+
}
170+
}
171+
ans as i64
172+
}
173+
}
174+
```
175+
158176
<!-- tabs:end -->
159177

160178
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn minimum_cost(s: String) -> i64 {
3+
let mut ans = 0;
4+
let n = s.len();
5+
let s = s.as_bytes();
6+
for i in 1..n {
7+
if s[i] != s[i - 1] {
8+
ans += i.min(n - i);
9+
}
10+
}
11+
ans as i64
12+
}
13+
}

solution/2700-2799/2716.Minimize String Length/README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ tags:
7272

7373
题目实际上可以转化为求字符串中不同字符的个数,因此,我们只需要统计字符串中不同字符的个数即可。
7474

75-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串的长度;而 $C$ 是字符集的大小,本题中字符集为小写英文字母,因此 $C=26$。
75+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,这里是小写英文字母,因此 $|\Sigma|=26$。
7676

7777
<!-- tabs:start -->
7878

@@ -104,8 +104,7 @@ class Solution {
104104
class Solution {
105105
public:
106106
int minimizedStringLength(string s) {
107-
unordered_set<char> ss(s.begin(), s.end());
108-
return ss.size();
107+
return unordered_set<char>(s.begin(), s.end()).size();
109108
}
110109
};
111110
```
@@ -143,6 +142,16 @@ impl Solution {
143142
}
144143
```
145144

145+
#### C#
146+
147+
```cs
148+
public class Solution {
149+
public int MinimizedStringLength(string s) {
150+
return new HashSet<char>(s).Count;
151+
}
152+
}
153+
```
154+
146155
<!-- tabs:end -->
147156

148157
<!-- solution:end -->

solution/2700-2799/2716.Minimize String Length/README_EN.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ tags:
100100

101101
### Solution 1: Hash Table
102102

103-
The problem can actually be transformed into finding the number of different characters in the string. Therefore, we only need to count the number of different characters in the string.
103+
The problem can actually be transformed into finding the number of distinct characters in the string. Therefore, we only need to count the number of distinct characters in the string.
104104

105-
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, the character set is lowercase English letters, so $C=26$.
105+
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. In this case, it's lowercase English letters, so $|\Sigma|=26$.
106106

107107
<!-- tabs:start -->
108108

@@ -134,8 +134,7 @@ class Solution {
134134
class Solution {
135135
public:
136136
int minimizedStringLength(string s) {
137-
unordered_set<char> ss(s.begin(), s.end());
138-
return ss.size();
137+
return unordered_set<char>(s.begin(), s.end()).size();
139138
}
140139
};
141140
```
@@ -173,6 +172,16 @@ impl Solution {
173172
}
174173
```
175174

175+
#### C#
176+
177+
```cs
178+
public class Solution {
179+
public int MinimizedStringLength(string s) {
180+
return new HashSet<char>(s).Count;
181+
}
182+
}
183+
```
184+
176185
<!-- tabs:end -->
177186

178187
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public:
33
int minimizedStringLength(string s) {
4-
unordered_set<char> ss(s.begin(), s.end());
5-
return ss.size();
4+
return unordered_set<char>(s.begin(), s.end()).size();
65
}
7-
};
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public int MinimizedStringLength(string s) {
3+
return new HashSet<char>(s).Count;
4+
}
5+
}

0 commit comments

Comments
 (0)