返回介绍

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

发布于 2024-06-17 01:03:01 字数 5359 浏览 0 评论 0 收藏 0

2716. Minimize String Length

中文文档

Description

Given a 0-indexed string s, repeatedly perform the following operation any number of times:

  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if any) and the closest occurrence of c to the right of i (if any).

Your task is to minimize the length of s by performing the above operation any number of times.

Return _an integer denoting the length of the minimized string._

 

Example 1:

Input: s = "aaabc"
Output: 3
Explanation: In this example, s is "aaabc". We can start by selecting the character 'a' at index 1. We then remove the closest 'a' to the left of index 1, which is at index 0, and the closest 'a' to the right of index 1, which is at index 2. After this operation, the string becomes "abc". Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.

Example 2:

Input: s = "cbbd"
Output: 3
Explanation: For this we can start with character 'b' at index 1. There is no occurrence of 'b' to the left of index 1, but there is one to the right at index 2, so we delete the 'b' at index 2. The string becomes "cbd" and further operations will leave it unchanged. Hence, the minimized length is 3. 

Example 3:

Input: s = "dddaaa"
Output: 2
Explanation: For this, we can start with the character 'd' at index 1. The closest occurrence of a 'd' to its left is at index 0, and the closest occurrence of a 'd' to its right is at index 2. We delete both index 0 and 2, so the string becomes "daaa". In the new string, we can select the character 'a' at index 2. The closest occurrence of an 'a' to its left is at index 1, and the closest occurrence of an 'a' to its right is at index 3. We delete both of them, and the string becomes "da". We cannot minimize this further, so the minimized length is 2.
 

 

Constraints:

  • 1 <= s.length <= 100
  • s contains only lowercase English letters

Solutions

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

class Solution:
  def minimizedStringLength(self, s: str) -> int:
    return len(set(s))
class Solution {
  public int minimizedStringLength(String s) {
    Set<Character> ss = new HashSet<>();
    for (int i = 0; i < s.length(); ++i) {
      ss.add(s.charAt(i));
    }
    return ss.size();
  }
}
class Solution {
public:
  int minimizedStringLength(string s) {
    unordered_set<char> ss(s.begin(), s.end());
    return ss.size();
  }
};
func minimizedStringLength(s string) int {
  ss := map[rune]struct{}{}
  for _, c := range s {
    ss[c] = struct{}{}
  }
  return len(ss)
}
function minimizedStringLength(s: string): number {
  return new Set(s.split('')).size;
}
use std::collections::HashMap;

impl Solution {
  pub fn minimized_string_length(s: String) -> i32 {
    let mut hash = HashMap::new();

    for c in s.chars() {
      hash.insert(c, true);
    }

    hash.len() as i32
  }
}

Solution 2

use std::collections::HashSet;

impl Solution {
  pub fn minimized_string_length(s: String) -> i32 {
    let mut set = HashSet::new();

    for c in s.chars() {
      set.insert(c);
    }

    set.len() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文