Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2712,2716 #4260

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ function minimumCost(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ function minimumCost(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
15 changes: 12 additions & 3 deletions solution/2700-2799/2716.Minimize String Length/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -104,8 +104,7 @@ class Solution {
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
```
Expand Down Expand Up @@ -143,6 +142,16 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 13 additions & 4 deletions solution/2700-2799/2716.Minimize String Length/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ tags:

### Solution 1: Hash Table

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.
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.

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$.
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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -134,8 +134,7 @@ class Solution {
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
```
Expand Down Expand Up @@ -173,6 +172,16 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
5 changes: 2 additions & 3 deletions solution/2700-2799/2716.Minimize String Length/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
};
5 changes: 5 additions & 0 deletions solution/2700-2799/2716.Minimize String Length/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}